开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜