`self.send` Causes ActiveRecord to Forget Timestamps

Hello everyone,

Has anyone run into errors with self.send in ActiveRecord models? I was working on this issue and narrowed it down to a specific function that calls self.send on an ApplicationRecord. Commenting out the code and replacing it with data manually to build up an object resolves the issue, but unfortunately we’re using it to check if attributes were updated via self.send("#{attribute}_change") if self.send("#{attribute}_changed?").

For me, it’s hard to say without taking a look at the code or a sample code. But here are some suggestions:

  1. Check if “#{attribute}_change” and “#{attribute}_changed?” methods return valid data.

  2. As I can see, you are returning the line self.send("#{attribute}_change") if self.send("#{attribute}_changed?") without any return keyword, that means this must be the very last line of the method definition, and if the if part is false, it will not evaluate self.send("#{attribute}_change"), in that case, it must return nil.

1 Like

The weird part is, I tested the object it’s building with .valid? (using our favorite debug method puts) and the object appears to be valid.

I’m busy at the moment, but I’m looking at refactored code I wrote months ago and am merging into the project that may or may not have stripped away the troublesome function. I’ll post back here ASAP.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs