在has_many关联中为“no children”创建named_scope
我想为没有post的博客提供一个named_scope。
以下不起作用。
class Post < ActiveRecord::Base belongs_to :blog end class Blog "blogs.id NOT IN (SELECT blog_id FROM posts)" end
你确定它不起作用吗? 这个对我有用。
不确定为什么你的代码不起作用。 是否错误返回您的期望? 你可以发布它生成的sql吗?
但是你可以尝试:
named_scope :has_no_posts, :include => [:posts], :conditions => "posts.id IS NULL"
- 如何使用Rails ActiveRecord进行LEFT OUTER JOIN?
- rails加入多态关联
- 没有ActiveRecord :: Base的连接池
- 如何编写仅查看日期时间字段的时间组件的activerecord查询?
- Heroku(PG ::错误:错误:列必须出现在GROUP BY子句中或用于聚合函数
- Rails中的高级SQL
- 有什么区别:before_save和:before_update Active Record回调?
- 在rubyonrails中,如何从ActiveRecord :: Relation对象获取关联的模型类?
- Rails 4:Model:Class的未定义方法`relation_delegate_class’