Zamierzam zapytać wszystkich użytkowników bez komentarzy w jednym zapytaniu sql?Rails User.joins.not (...) w Active Record?
Modele:
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
end
Więc chcę przeciwieństwo tego:
User.joins.(:comments).group('users.id')
Ale nie tak: (ponieważ generuje dwa zapytań)
User.where.not(id: Comment.pluck(:user_id))
może coś podobnego to?
User.joins.not.(:comments).group('users.id')
Dzięki za wszelkie dane wejściowe!
Spróbuj 'User.includes (: komentarze) .gdzie (komentarze: {id: nil})' – MrYoshiji