Misha Moroshko
Misha Moroshko

Reputation: 171351

Rails: How filter all objects with specific parameter?

Is there a shorter way to write this ?

Job.all(:conditions => "job_source_id=1")

Upvotes: 2

Views: 2169

Answers (3)

Unixmonkey
Unixmonkey

Reputation: 18784

I usually like to use scopes for this kind of thing like so:

# in the model
scope :from_sales, :conditions => { :job_source_id => 1 }

Then, from anywhere, I can just call:

Job.from_sales.all

This lets me express myself in my problem domain instead of sql.

Upvotes: 0

macarthy
macarthy

Reputation: 3069

Use the Dynamic Finders

http://guides.rubyonrails.org/active_record_querying.html#dynamic-finders

Job.find_by_source_id(1)

Upvotes: 1

Andrew Marshall
Andrew Marshall

Reputation: 96944

A little shorter and more readable:

Job.where :job_source_id => 1

Upvotes: 5

Related Questions