このSQLコードと同等のRubyonRailsでコントローラーにフィルターを作成する方法
select * from persons where persons.category = 'developers'
このSQLコードと同等のRubyonRailsでコントローラーにフィルターを作成する方法
select * from persons where persons.category = 'developers'
オタクを取得するために、名前付きスコープの作成を検討することをお勧めします。
class Person < ActiveRecord::Base
scope :developers, where(category: 'developers')
end
コントローラ内:
before_filter :developers_only
private
def developers_only
@people = Person.developers
end
これを使って:
before_filter :nerds_only
private
def nerds_only
@people = Person.where(:category => 'developers')
end