First, some background. I have a
Payment model that
has_many :subscriptions. When a user pays (creates a new payment), I have an
after_create callback that adds the newly created
@subscriptions.payment_id. Here's what it looks like:
def update_subscription @unpaid_subs = Subscription.where(:user_id => self.user_id).unpaid @unpaid_subs.each do |sub| sub.payment_id = self.id sub.start # call to state_machine to change state from pending -> active sub.save end end
I know that doing database queries inside a loop is generally not good for performance, but I don't know any way to update multiple records at the same time. Also, is there a way to pass the
@unpaid_subs instance variable from my create action to the callback (it's the same query on both) so that I can remove the query here?