Reputation: 9925
How to write filter on the controller in Ruby on Rails, that is eqvivalent to this SQL code
select * from persons where persons.category = 'developers'
Upvotes: 0
Views: 121
Reputation: 3919
Use this:
before_filter :nerds_only
private
def nerds_only
@people = Person.where(:category => 'developers')
end
Upvotes: 3
Reputation: 1301
You may want to consider creating a named scope to get the nerdies:
class Person < ActiveRecord::Base
scope :developers, where(category: 'developers')
end
In your controller:
before_filter :developers_only
private
def developers_only
@people = Person.developers
end
Upvotes: 3