質問

Paginated Find Callのような方法を実行する方法があります。

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なぜこのエラー?また、次のように渡すにはどうすればよいですか。Per_Pageパラメーターをadange_scopeのクエリを制限します。

ありがとう

役に立ちましたか?

解決

ユースケースは、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, 、したがって、供給する必要はありません :all 結果セットスコープ予選 paginate 方法。

時間値は、クエリの実行時に計算されます。これは、同じ効果のためにラムダを使用する必要がある名前_scopeとは異なります。

Andage_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'
               }}

これで、次のようにdamed_scopeを使用できます

Post.feature_all.paginate(:per_page => Constants::LISTINGS_PER_PAGE, 
                          :page => page)

他のヒント

名前付きスコープにはありません : 注文キーの前。それは指名された範囲を吹き飛ばしているかもしれません。そうでなければ、それはうまく見えます。

とはいえ、ラムダを使用して現在の時刻に関連するレコードを取得する必要はありません。データベースは、同様の方法を提供します。たとえば、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() UTCの現在の日付と時刻を返すMySQL固有の関数です(このRailsは、すでにデータタイムを保存する必要があります)。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top