我有一个叫做作者的模型.作者有很多
文章.
文章有
一个称为.published的范围,它的作用是:where(published:true).
我想加载作者,发表文章.
我试过了:
Author.includes(:articles.published).find(params[:author_id])
但是会抛出一个错误:undefined方法’published’.
任何想法?
我认为最好的
解决办法是:
Author.includes(:articles).where(:articles=>{published: true}).find(params[:author_id])
或者你可以创建范围:
class Author < ActiveRecord::Base
scope :published_articles,-> { includes(:articles).where(articles: { published: true}) }
end
接着:
Author.published_articles.find(params[:author_id].to_s)