我有一个叫做作者的模型.作者有很多文章.
文章有一个称为.published的范围,它的作用是:where(published:true).
文章有一个称为.published的范围,它的作用是:where(published:true).
我想加载作者,发表文章.
我试过了:
- Author.includes(:articles.published).find(params[:author_id])
解决方法
我认为最好的解决办法是:
- 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)