chief
chief

Reputation: 1

Rails - conditions for find

@people = Person.find(:all, :conditions => ['parent_id = :parent_id', params[:person]])

I would like to integrate an age range condition based on birthdate as well. I figure in the model I can write something like:

def minimum_age_conditions
  ["people.birthdate <= ?", Date.today - minimum_age.years] unless minimum_age.blank?
end

def maximum_age_conditions
   ["people.birthdate >= ?", Date.tomorrow  - (maximum_age+1).years] unless maximum_age.blank?
end

So how can I link the controller conditions to these methods in the model? Or is it better to place these all in the conditions part of the find?

Upvotes: 0

Views: 146

Answers (1)

Jimmy
Jimmy

Reputation: 37081

Create a named scope in your model and put all your logic there. Then back in the controller, you just do:

@people = Person.my_scope

http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html

Upvotes: 1

Related Questions