jeudi 16 juin 2016

Upgrading from Rails 3 to Rails 4 now belongs_to associations are returning an ActiveRecord::Relation?

After upgrading a Rails 3 project to Rails 4 any queries using a belongs_to association are returning an ActiveRecord::Relation instead of the actual ActiveRecord model object.

As an example if I have the following association.

belongs_to :project

then previously I would call

foo.project

this would return the project object that foo "belongs_to"; now instead it is returning an ActiveRecord::Relation so everywhere I would have to switch to

foo.project.first

It seems like this is not proper behavior as a belongs_to association would only ever return one object. Am I missing something here?

Aucun commentaire:

Enregistrer un commentaire