An application I work on is using Rails 3.2.18 and Active Record 3.2.18. It was using Ruby 2.0.0, but someone from DevOps needed to upgrade to Ruby 2.2.4 today. Once users started making traffic on the site, everything started to explode. The administrators cant even access Active Admin.
After digging into this for quite some time, I realized I was thrown off by an error I was constantly seeing:
NoMethodError: undefined method `name' for nil:NilClass`
Since some of my models have a name
attribute, I assumed this was the culprit.
Long story short, the issue is with calling size
on an AR has_many
relation that has not yet been loaded into memory. Here is how I can demonstrate this in rails console:
model_instance = MyModel.find(12345)
model_instance.some_relations.size
# the error from above will be thrown (nomethoderror on name: for nil)
model_instance.some_relations
model_instance.some_relations.size
# it works fine and produces expected output
Also, model_instance.some_relations.count
will always work (though I do understand the differences between count
and size
).
I have found surprisingly little on this issue. There is a related GitHub issue here, but I cant prove its the same issue I am having.
I am going to rollback my release which upgrades to Ruby 2.2.4 for now, but was wondering if anyone on SO could shed some light on this. Is there a bug between Active Record 3.2.18 and Ruby 2.2.* ? Is the only solution, assuming I need to upgrade to Ruby 2.2.4, to upgrade Rails as well?
Aucun commentaire:
Enregistrer un commentaire