开发者

Rails: How filter all objects with specific parameter?

开发者 https://www.devze.com 2023-02-15 05:02 出处:网络
Is there开发者_开发问答 a shorter way to write this ? Job.all(:conditions => \"job_source_id=1\")

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消