我正在使用Ruby on Rails 3.2.2,並且正在試驗Squeel gem。我想知道如果(以某種方式,使用Squeel gem或不),可以在where
子句中「直接」添加與scope
方法相關的SQL子句。也就是說,我有:是否可以在`where'方法/子句中「添加」與`scope`方法有關的SQL子句?
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 ...
我tryed sifters但那些(至少對我來說),意在在其他Squeel聲明中僅使用。也就是說,如果我聲明一個篩選器,那麼我不能使用它來範圍ActiveRecord
s,因爲該篩選器返回Squeel::Nodes::Predicate
對象而不是ActiveRecord::Relation
。