[英]Rails: How filter all objects with specific parameter?
有写这个的更短方法吗?
Job.all(:conditions => "job_source_id=1")
简短一些,可读性强:
Job.where :job_source_id => 1
Use the Dynamic Finders 使用动态查找器
http://guides.rubyonrails.org/active_record_querying.html#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. 这使我可以在问题域而不是sql中表达自己。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.