我有车型像:轨道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()后使用它 –