Tôi đang tìm cách truy vấn tất cả Người dùng không có Nhận xét trong một truy vấn sql đơn lẻ?Rails User.joins.not (...) trong Active Record?
Models:
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
end
Vì vậy, tôi muốn điều ngược lại điều này:
User.joins.(:comments).group('users.id')
Nhưng không như thế này: (vì nó tạo ra hai truy vấn)
User.where.not(id: Comment.pluck(:user_id))
Có lẽ cái gì đó như điều này?
User.joins.not.(:comments).group('users.id')
Cảm ơn mọi đầu vào!
Hãy thử 'User.includes (: comments) .where (bình luận: {id: nil}) ' – MrYoshiji