轨道中的范围和范围
有人可以解释这种方法的作用以及我可以传递给它的内容吗?
scoped(options = nil) Returns an anonymous scope.
还有范围方法的作用吗? 阅读文档后我不明白。
在ActiveRecord中,所有查询构建方法(如where
, order
, joins
, limit
等)都返回一个所谓的范围 。 只有在调用类似all
的kicker方法时,或者first
执行构建查询并返回数据库的结果。
scoped
类方法也返回范围。 返回的范围默认为空,这意味着结果集不会受到任何限制,这意味着如果执行查询,将返回所有记录。 您可以使用它来提供一个“空”替代方案,例如MurifoX的query_by_date示例。 或者您可以使用它将多个条件组合到一个方法调用中,例如:
Model.scoped(:conditions => 'id < 100', :limit => 10, :order => 'title ASC') # which would be equivalent to Model.where('id < 100').limit(10).order('title ASC')
scope
类方法允许您定义也返回范围的类方法,例如:
class Model scope :colored, lambda {|col| where(:color => col) } end
可以像这样使用:
Model.colored
范围的好处在于您可以根据需要(几乎)组合它们,因此以下是绝对可能的:
Model.red.where('id < 100').order('title ASC').scoped(:limit => 10)
我还强烈建议您阅读http://guides.rubyonrails.org/active_record_querying.html
我在过去使用过它。当您对ActiveRecord
查询接口进行链式调用时,如下所示:
Model.where(:conditions).where(:more_conditions).where(:final_conditions)
它们中的每一个都已经成为范围,使链条工作没有任何问题。 但是,假设您有类似这样的事情:
Model.query_by_date(date).query_by_user(user).query_by_status(status) scope :query_by_date, lambda { |date| case date when "today" where(:date => Date.today) when "tomorrow" where(:date => Date.tomorrow) else # Any value like '' or 0 or Date.whatever end }
如果日期参数不是今天或明天,这将导致错误。 它会选择最后一个值并尝试使用下一个query_by_user
链接此查询,从而导致undefined method default_scoped? for ''
undefined method default_scoped? for ''
。 但是如果你在else
条件中放置一个scoped
方法,它将没有任何缺陷,因为你说activerecord你通过这个方法/命名范围并且没有调用where/find/other activerecord methods
,但返回了一个范围对象,所以你可以继续链接查询和东西。
最终会是这样的。
else scoped end
希望你能理解这个简单的例子。