Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Is there an order in which methods are fired in rails for a given callback?

I have a couple of methods that fire after_update but one requires the former to execute as it changes the state of the an instance stored in a variable established by another method fired during a before_update callback.

3 Answers

Callbacks should fire in the order that you define them.

If you have:

after_update :send_email
after_update :fire_the_rockets

Then, send_email will be called before fire_the_rockets.

It's generally not a great idea to make two methods dependent on each other like this. Maybe you can restructure the code to avoid this issue entirely.

Hi Andrew,

Awesome, thanks. The reason I'm doing it is because when I try to pass a variable, in this case @party_id that I establish before_update to after_save or after_commit it doesn't work. Any idea why?

Best, Damian

It might be easier for me to understand with a bit more context, can you paste the code here?

Thanks a bunch for the help.

        # before_update
        def establish_party
            @party = Party.find(id = self.party_id)
        end

        # after_update and afer_destroy
        def update_rsvps_left
            count = Rsvp.where("party_id = ? AND state = ?", @party.id, "accepted").count
            @party.rsvps_left = party.rsvps_available - count
            @party.save
        end

        #after_update
        def cancel_rsvps_if_full
            if @party.state == "full"
                rsvps = Rsvp.where("party_id = ? AND state = ?", @party.id, "pending")
                if !rsvps.blank?
                    rsvps.each do |r|
                        r.update_column(:state, "cancelled")
                    end
                end
            end
        end

cancel_rsvps_if_full is the reason I asked this question. It wouldn't run in after_save or after_commit

It seems like this model should have an association to Party

If it does, you should be able to do something like:

belongs_to :party

...

# after_update and afer_destroy
def update_rsvps_left
  count = Rsvp.where("party_id = ? AND state = ?", party.id, "accepted").count
  party.rsvps_left = party.rsvps_available - count
  party.save
end

#after_update
def cancel_rsvps_if_full
  if party.state == "full"
    Rsvp.where("party_id = ? AND state = ?", party.id, "pending").each do |r|
      r.update_column(:state, "cancelled")
    end
  end
end

You might also consider moving these methods into a service object and that would remove the need for the callbacks entirely.

Thanks! Not sure why I thought I need to establish the id beforehand... It works in after_save now. I'll read up on service objects now.