Rails 3 finding parents which have no child

前端 未结 3 827
遇见更好的自我
遇见更好的自我 2020-12-06 18:57

In a one to many relationship with no counter cache how can I find parents with no child?

user.rb

has_many :pages

page.rb



        
3条回答
  •  不知归路
    2020-12-06 19:34

    One way would be

    User.where("(SELECT COUNT(*) FROM pages WHERE pages.user_id = users.id) = 0")
    

    But I'm not sure how (in)efficient that would be.

提交回复
热议问题