Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
MissingAttributeError for database column in after_find callback #3313
Under some circumstances (which are not clear to me), calling self.column_name in an after_find throws a MissingAttributeError even when column_name is a valid db column. In particular, I have a project with a milestones table that has a billing_invoice field where
fails with a MissingAttributeError for billing_invoice within the after_find callback but
succeeds. An essentialized version of the project with steps to repro the bug can be found at https://github.com/shlevy/after-find-bug
invoiceable_model.lib is only there to reduce code repetition. It is very small, literally all it does is define two functions, without overriding any core class or anything. Here is the code, can you explain how it is causing this problem?
you need to be very careful when you use
Milestone.find 1, include: [project: [:time_logs]], order: "projects.name"
ActiveRecord executes the following sql:
you can see, that the first call only fetches the ID, to fetch the complete records later on. This has the effect, that AR first instantiates a Milestone Model with only the ID present. The
def set_invoice return unless attributes.has_key?(:billing_invoice) ... end
The second example that worked:
Milestone.all conditions: "milestones.id = 1", include: [project: [:time_logs]], order: "projects.name"
results in the following sql:
this time, everything is loaded from the beginning and as such, the code works as expected.
I don't think this is a bug but rather the functionality how AR works. I don't suggest to use
@steveklabnik I don't think this is a bug, what do you think?