Don’t overwrite Rails guts

In getting ready to load a bunch of new trades into my database of stock trades by members of Congress, I was troubled to see that ActiveRecord was no longer working the way it was supposed to. For one of  my models, every call to save prompted a false, which I noticed when I realized that none of the data I was trying to put was being saved. (This was a reminder that I should use save! for these data collection purposes.) At first I thought maybe the database was locked for some reason, but then I realized that saving worked on other models/tables, so that couldn’t be it. It was acting like a validation was failing, but I don’t have any validations for this model. I cursed and googled for a solution in vain.

Finally I looked into the ActiveRecord source code, which seemed to be not going anywhere until I saw that a key part of the validation process is a method called valid? For some reason I remembered that I had written a valid? method for this model (it checks to see if there is a company and a date for this trade) and I realized this must be it: none of the trades I am trying to put in have a company (that association comes later) so because they are not valid? Rails thinks they fail the validations.   

So: I will try not to stomp on the ActiveRecord::Base code again. It’s a tricky bug to fix when you do.