eomeroff
eomeroff

Reputation: 9925

RoR filter on controller as this SQL

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

Answers (2)

Anil
Anil

Reputation: 3919

Use this:

before_filter :nerds_only

private

def nerds_only
  @people = Person.where(:category => 'developers')
end

Upvotes: 3

Henrique Zambon
Henrique Zambon

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

Related Questions