4

I'm having a weird issue for which I can't find a logical explanation.

I'm investigating a bug and put some logging in place (through Rollbar) so I can see the evolution some instances of one of my models.

Here is what I got:

class Connexion < ActiveRecord::Base
    before_validation :save_info_in_rollbar
    after_save :save_info_in_rollbar

    def save_info_in_rollbar
      Rollbar.log("debug", "Connexion save", :connexion_id => self.id, :connexion_details => self.attributes)
    end
end

Now I am getting loads of data in rollbar (pretty much 2 rows for every time a connexion is created/updated). But the weird thing is the following: for some connexions (=> exactly the ones with faulty data which I am investigating), I am getting no data at all!

I don't get how it's possible for a connexion to be created and persisted to the DB, and not have any trace of the before_validation logging. It looks like the callback is not called, but unless I'm mistaken, it's supposed to be the first one in the callback order => what could prevent it from being called?

EDIT >> Copy and paste from a reply, might be relevant: There are 3 cases in which a connexion is created or updated, and thoses cases are :

  • .connexions.create()
  • connexion.attr = "value"; connexion.save!
  • connexion.update_attributes(attr: "value")
BPruvost
  • 503
  • 2
  • 5
  • 16
  • Try `Connexion.new(params).valid?`. What you'll see? – Alex Antonov Jun 12 '15 at 13:11
  • irb(main):022:0> Connexion.new.valid? [Rollbar] Scheduling payload [Rollbar] Details: https://rollbar.com/instance/uuid? [Rollbar] Sending payload => true irb(main):023:0> [Rollbar] Success => Model is valid & data is sent to rollbar => all is good – BPruvost Jun 12 '15 at 13:13
  • Maybe, `Rollbar.log` just do nothing when you got `:connexion_id => nil`? Try to puts or raise something in your `save_info_in_rollbar` method – Alex Antonov Jun 12 '15 at 13:13
  • It is actually logging without an ID (i can see the data as well, just that the ID column is nil) – BPruvost Jun 12 '15 at 13:14
  • Try to raise or puts something. Is there still unexpected behavior? – Alex Antonov Jun 12 '15 at 13:17

3 Answers3

4

The only cases in which the callback won’t be run are:

But: I might be missing a case. Also, I’m assuming there isn’t a bug in ActiveRecord/ActiveModel causing this.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
1

Sorry about the stupid question guys, the explanation was that we were having 2 apps working on the same database, and the modification was made by the other app (which of course was not sending the Rollbar updates).

Sometimes the toughest issues have the most simple answers haha

BPruvost
  • 503
  • 2
  • 5
  • 16
0

Firstly, you don't need self in instance methods, as the scope of the method is instance.

Secondly, you need to check, how are you saving the data to the database. You can skip callbacks in Rails: Rails 3 skip validations and callbacks

Thirdly, double check the data.

Community
  • 1
  • 1
  • 1/ I know. It's a convention we have to put self. when dealing with attributes so that we can make the difference between attributes and variables more easily ;) 2/ There are 3 cases in which a connexion is created or updated, and thoses cases are : - .connexions.create() - connexion.attr = "value"; connexion.save! - connexion.update_attributes(attr: "value") 3/ My data is corrupted. That's why I am doing some debugging in the first place ;) – BPruvost Jun 12 '15 at 13:16