我有一个方法,它执行分页的find调用,比如..
1
coll = paginate(:all, lambda {{:conditions => ['status = ? AND expires < ?', 'a', DateTime.now]}}, :select => Constants::POST_FIELDS,
:order => 'posts.ratings DESC', :include => [{:poster => :poster_friends}, :category], :per_page => Constants::LISTINGS_PER_PAGE,
:page => page)当此调用运行时,它只是忽略生成的查询中的条件。
2
如果我在named_scope中尝试它,我得到一个错误,如..
named_scope :featured_all, lambda {{:conditions => ['status = ? AND expires < ?', 'a', DateTime.now], order => 'posts.ratings DESC', :include => [{:poster => :poster_friends}, :category],
:select => Constants::POST_FIELDS}}命名作用域错误:
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/hash/keys.rb:47:in `assert_valid_keys'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2101:in `with_scope'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:113:in `__send__'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:113:in `with_scope'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:174:in `method_missing'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:188:in `load_found'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:166:in `proxy_found'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:109:in `is_a?'
/usr/lib/ruby/gems/1.8/gems/hirb-0.3.1/lib/hirb/formatter.rb:78:in `determine_output_class'
/usr/lib/ruby/gems/1.8/gems/hirb-0.3.1/lib/hirb/formatter.rb:48:in `format_output'
/usr/lib/ruby/gems/1.8/gems/hirb-0.3.1/lib/hirb/view.rb:213:in `render_output'
/usr/lib/ruby/gems/1.8/gems/hirb-0.3.1/lib/hirb/view.rb:126:in `view_output'对于#1,我做错了什么,为什么它忽略了条件?
对于#2,为什么会出现这个错误?另外,如何传递:page和per_page参数来限制named_scope中的查询。
谢谢
发布于 2010-08-16 06:33:57
您的用例不保证使用named_scope。你可以像下面这样写一个常规的查找器:
coll = paginate(:select => Constants::POST_FIELDS,
:conditions => ['status = ? AND expires < ?', 'a', DateTime.now],
:include => [{:poster => :poster_friends}, :category],
:order => 'posts.ratings DESC',
:per_page => Constants::LISTINGS_PER_PAGE,
:page => page)paginate方法等同于all,因此您不必向paginate方法提供:all结果集范围限定符。
时间值是在执行查询时计算的。这不同于named_scope,您必须使用lambda才能达到相同的效果。
如果需要使用named_scope,请执行以下操作:
named_scope :featured_all, lambda {{
:select => Constants::POST_FIELDS,
:conditions => ['status = ? AND expires < ?', 'a', DateTime.now],
:include => [{:poster => :poster_friends}, :category],
:order => 'posts.ratings DESC'
}}现在,您可以按如下方式使用named_scope
Post.feature_all.paginate(:per_page => Constants::LISTINGS_PER_PAGE,
:page => page)发布于 2010-08-16 03:50:18
您的命名作用域在订单键之前缺少:。这可能会破坏指定的作用域。否则,它看起来很好。
也就是说,您不需要使用lambda来获取与当前时间相关的记录。您的数据库提供了类似的方法。例如,如果您使用的是MySQL,则可以使用:
named_scope(:featured_all, {
:conditions => "status = 'a' AND expires < UTC_TIMESTAMP()",
:order => 'posts.ratings DESC',
:include => [{:poster => :poster_friends}, :category],
:select => Constants::POST_FIELDS
})UTC_TIMESTAMP()是一个特定于MySQL的函数,它以协调时为单位返回当前日期和时间(哪个Rails应该已经存储了您的DateTimes )。
https://stackoverflow.com/questions/3488534
复制相似问题