`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.

thanks my issue has been fixed.

I’ve actually narrowed this down further and realized it’s from self.employment_histories << employment_history. Even adding an EmploymentHistory with attributes assigned beforehand (a Hash with assign_attributes) there’s an error.

The problem disappears in Rails 6.1, so we are just forgetting about it for now.