3

Ruby on Rails 3.2.2 を使用しており、Squeel gem を試しています。(何らかの方法で、Squeel gem を使用するかどうかによって)scopeメソッドに関連する SQL 句を句に「直接」 「追加」できるかどうかを知りたいですwhere。つまり、私は持っています:

class Article < ActiveRecord::Base
  # Note: This is a scope method.
  def self.created_by(user)
    where(:user_id => user.id)
  end

  # I would like to use a scope method like the following.
  #
  # Note: Code in the following method doesn't work, but it should help
  # understanding what I mean.
  def self.scope_method_name(user)
    where{ created_by(user) | ... & ... }
  end
end

したがって、実行するArticle.scope_method_name(@current_user).to_sqlと、次のようなものが返されます。

SELECT articles.* FROM articles WHERE articles.user_id = 1 OR ... AND ...

私はシフターを試しましたが、それらは (少なくとも私にとっては) 他の Squeel ステートメントでのみ使用されることを意図してますつまり、sifter を記述した場合、それを使用してActiveRecords をスコープすることはできません。その sifterSqueel::Nodes::PredicateActiveRecord::Relation.

4

2 に答える 2

1

OR操作のために、より生のARELにドロップダウンする必要があります

def self.scope_method_name(user)
  t = arel_table
  where(
    (t[:user_id].eq(user.id).or(
    t[:blah].eq('otherthing')
      ).and([:bleh].eq('thirdthing'))
    )
end

または、それらの線に沿った何か。

于 2012-06-26T12:03:07.403 に答える