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 を記述した場合、それを使用してActiveRecord
s をスコープすることはできません。その sifterSqueel::Nodes::Predicate
はActiveRecord::Relation
.