Rails: How filter all objects with specific parameter?
Is there开发者_开发问答 a shorter way to write this ?
Job.all(:conditions => "job_source_id=1")
A little shorter and more readable:
Job.where :job_source_id => 1
Use the Dynamic Finders
http://guides.rubyonrails.org/active_record_querying.html#dynamic-finders
Job.find_by_source_id(1)
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.
精彩评论