i have following models:
notification belongs_to :receiver, class_name: 'user' # has sent_email boolean column default set false user has_many :received_notifications, class_name: 'notification', foreign_key: 'receiver_id', inverse_of: :receiver has_one :alert alert belongs_to :user # has frequency integer column
i want grab notifications sent_email false users set alert frequency 1, , want return result this:
[ {user object => [<all of notifications user 1>]}, {user object => [<all of notifications user 2>]} ]
i want 1 or 2 queries @ most.
what activerecord query like?
you can use includes
.
u = user.includes(:received_notifications, :alert) .where("alerts.frequency_discussion_alerts = ? , notifications.sent_email = ?", 1, false) .references(:notifications,:alerts)
now in batch, can fetch users , job.
u.find_each { |u| u.received_notifications }
Comments
Post a Comment