@people = Person.find(:all, :conditions => ['parent_id = :parent_id', params[:person]])
我想整合基于出生日期的年龄范围条件。我在模型中可以写出类似的东西:
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
那么如何将控制器条件链接到模型中的这些方法?或者将这些全部放在查找的条件中是否更好?
答案 0 :(得分:1)
在模型中创建一个命名范围,并将所有逻辑放在那里。然后返回控制器,你只需:
@people = Person.my_scope
http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html