如何在Ruby on Rails中在控制器上编写过滤器,这相当于这个SQL代码
select * from persons where persons.category = 'developers'
答案 0 :(得分:3)
使用此:
before_filter :nerds_only
private
def nerds_only
@people = Person.where(:category => 'developers')
end
答案 1 :(得分:3)
您可能需要考虑创建一个命名范围来获取神经质:
class Person < ActiveRecord::Base
scope :developers, where(category: 'developers')
end
在您的控制器中:
before_filter :developers_only
private
def developers_only
@people = Person.developers
end