我有車型像:軌道4,對相關模型範圍加載所有enties(範圍不過濾)
class Post < AvtiveRecord::Base
belongs_to :article
default_scope { order(created_at: :desc) }
scope :active, -> { where(active: true).order(created_at: :desc) }
scope :where_author, -> (author) { where("author LIKE ?", "#{author}%") }
end
class Article < ActiveRecord::Base
has_many :posts, dependent: :destroy
end
當軌控制檯上我嘗試:
Article.find(123).posts.where_author("guest")
我得到預期值。
但是當我這樣做在ArticlesController:
@articles = Article.includes(:posts).posts.where_author("guest") # I will use params array when it work
這會將所有文章,而忽略範圍的條件下,實際的SQL查詢完全不包括範圍的一部分。
我試過joins
和includes
,結果相同。
我在做什麼錯了?
謝謝。
但要使用鏈接像 'Article.find(123).posts.where_author(「guest」).active' 我仍然必須使用範圍和lambdas? – rolkos
如果'active'是一個文章範圍,那麼在關閉merge()後使用它 –