Ruby on Rails is an open-source web development framework optimized for programmer happiness and maintaining sustainable productivity. Many things are defined by convention, freeing you from having to re-invent things to stay productive.
-2
votes
0answers
39 views
user has_role? please help me to review my code [closed]
this is the user model code:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and ...
0
votes
1answer
57 views
Math Calculus in ruby
I have several calculus to do in my ruby app.
My code is currently working but I find it very ugly.
@guestreviews = GuestReview.where(:reviewed_id => @user.id)
@hostreviews = ...
1
vote
1answer
43 views
Rails: Setting a transient attribute on a set of objects from one model based on information from a junction model
I have a user model, a task model, and a junction model user_task that saves the data joining the two:
class Task < ActiveRecord::Base
attr_accessor :completed_on
has_many :user_tasks
class ...
3
votes
1answer
127 views
Nasty Age Printing Method
I have this ugly age printing method. Can you do better?
def age(birth_date)
today = Date.today
years = today.year - birth_date.year
months = today.month - birth_date.month
(months -1) if ...
1
vote
4answers
65 views
can this rails code be simplified and be more efficient? nested if else statements
Here's a part of my controller and it's getting quite lengthy (the code works).
Would this code slow down the performance of my website? Can it be cleaned up and be written more efficiently?
def ...
1
vote
0answers
25 views
Converting data in a Rails Migration using the model
Lets imagine we have an Article entity, with data imported from a legacy database. It contains an appendix column with type integer. This column is always filled with values 0, 1 or nil, so we decide ...
2
votes
1answer
110 views
Refactor Rails middleware initialization block
I'm working on a simple superfeedr powered rails app here.
Based on the superfeedr-rack gem documentation, I'm doing this to initialize the middleware (snippet from application.rb config block):
...
0
votes
1answer
36 views
Rails 3: Create method with association?
In Rails 3, I have two models, Nutrient and Recommendation. Nutrient has_many Recommendation, Recommendation belongs_to Nutrient. I am trying to implement the create method of the recommendation in ...
2
votes
3answers
70 views
I need help refactoring some Rails code that looks clunky.
I would like to refactor this block, it looks clunky:
# refactor me
receive_payment_on = false
config[:sections].each do |section|
if section[:applicants]
section[:applicants][:sections].each ...
2
votes
1answer
53 views
two or more render/redirect in the same method
I often have method like this one with two or more render to do due to catching the error for example.
I currently do something like this:
def update
@user = current_user
if ...
0
votes
1answer
29 views
Ruby on Rails Single Table Inheritance [closed]
I'm trying to set up single table inheritance in Rails. I have single table called Finances, with a type set to Income or Expense.
My code is here: https://gist.github.com/caser85/5096447
Ok, I got ...
3
votes
2answers
100 views
How would one more elegantly parse data from XML using Ruby and Nokogiri?
I have a method that parses XML into an array of hashes.
Here is the original XML:
<rowset name="skillqueue" key="queuePosition" ...
0
votes
1answer
57 views
Underscore-case version of ActiveRecord model's name
Is there a better way to get the underscore-case version of an ActiveRecord model's name? So far this works, but it's far from ideal:
my_active_record_instance.class.name.underscore
2
votes
1answer
46 views
Rails helper method refactor
I have this messy helper method:
def gesture(klass, item, text, desc)
element_class = klass.to_s + " gesture"
content_tag :li do
if klass == :sell
link_to new_reply_path(item_id: ...
2
votes
1answer
35 views
Calculate sum of price on distant relation
If i have a booking, with a method total, which calculates the total price of the booking from the sum of all the prices of activities that belong to appointments, where a booking hasmany appointments ...
0
votes
1answer
32 views
What is the best way to format a large has_many through line?
This question is about code style.
I have this line in one of my models:
has_many :owning_artists, :through => :artist_tracks, :source => :artist, :conditions => { :artist_tracks => { ...
2
votes
1answer
40 views
Refactor Ruby ActiveRecord importing library
In my Rails app I need to import some files from CSV and Excel files. I needed it in 2 models so I have written lib/importable.rb:
module Importable
def self.included(base)
base.send :extend, ...
1
vote
1answer
88 views
How can I make this code DRY and KISS in my model?
class Rating < ActiveRecord::Base
attr_accessible :item_type, :item_id, :rating, :voters_up, :voters_down
serialize :voters_up, Hash
serialize :voters_down, Hash
belongs_to :ranks, ...
3
votes
2answers
80 views
How can I make this code DRY in my controller?
What is the best way to DRY this code up?
class NotificationsController < ApplicationController
before_filter :load_user
def unsubscribe
if @facebook_admin && ...
0
votes
1answer
138 views
Why did I fail this Ruby developer test? [closed]
I took this developer test for a potential employer, and they said I failed because of my response to the first question. But Rails for Zombies seemed to imply that that's how you assign data to ...
3
votes
2answers
107 views
An abundance of ternary operators
I'd like to find a way to do what this method does in a cleaner, less hacky looking way
def self.create_from_person!(person)
spi = new(:person => person, :provider => person.provider)
...
2
votes
1answer
22 views
How to refactor that content_tag adding method?
How to get rid of that duplicaiton in if conditional?
def set_caption(result)
if result.respond_to?(:app_name)
content_tag(:div, result.type_name, class: 'type-name') +
content_tag(:div, ...
1
vote
1answer
52 views
Updating / validating mass update rails association
My recipes model is as follows
has_many :quantities
has_many :ingredients, :through => :quantities, :uniq => true
has_many :sizes, :through => :quantities, :uniq => true
And the ...
3
votes
3answers
89 views
Best way to include image_tag inside link_to
What is the most elegant and readable way to in include (potentially long) image_tag calls inside of link_to?
Example
<%= link_to image_tag('buckminsterfullerene.png', width: '210', height: '60', ...
3
votes
2answers
89 views
Can I improve the way this model RSpec is written?
I'm new to RSpec and testing in general. I've come up with a spec for testing my Content model and I need some feedback 'cause I think there are many improvements that can be done. I don't know if the ...
4
votes
2answers
169 views
Carousel code review
Is there a better way to implement my carousel in less lines? Currently my solution is:
<div id="myCarousel" class="carousel slide">
<div class="carousel-inner">
<% ...
3
votes
1answer
104 views
Rails: Retrieve all parents, all possible children, sorted and with their “depth”
I have a model section with a column parent_id - the only thing identifying a parent/child relationship. I wrote a helper method to output all sections, all their possible subsections and include ...
2
votes
2answers
95 views
Placement of success code in a conditional
I'm not sure if there's a standard view where the placement of a success result should be in a conditional that can return multiple statuses. The success condition of this function is in the middle of ...
-1
votes
1answer
70 views
How to create 3 objects using one form only if each one is valid? [closed]
I want to create Job, Employer and Company using one form.
Model
class Job < ActiveRecord::Base
accepts_nested_attributes_for :company, :employer
belongs_to :company
belongs_to :employer
...
4
votes
1answer
105 views
Refactor ruby each-code
def users_list
html = ''
self.users.each do |user|
html << user.link_avatar_tag
end
html.html_safe
end
I feel that it is possible to write shorter.
Is it possible ...