作为此SQL的控制器上的RoR过滤器

时间:2012-06-01 14:37:48

标签: ruby-on-rails action-filter

如何在Ruby on Rails中在控制器上编写过滤器,这相当于这个SQL代码

select * from persons where persons.category = 'developers'

2 个答案:

答案 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