【发布时间】:2015-04-19 08:08:51
【问题描述】:
我有这样的模型:
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
在 Rails 控制台上我尝试:
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 进行了尝试,结果相同。
我做错了什么?
谢谢。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 scope eager-loading