How is this possible? All I have done in the console are create these objects and associate them. This makes no sense to me. p.jobs.first and j are the same thing. Why does the title method work on j and not on p.jobs.first?
ruby-1.9.2-p136 :031 > p
=> #<Person id: 14, type: "Person", desc: nil, created_at: "2011-01-24 23:53:13", updated_at: "2011-01-24 23:53:13">
ruby-1.9.2-p136 :032 > p.jobs.first
=> #<Job id: 18, type: "Job", created_at: "2011-01-24 23:53:36", updated_at: "2011-01-24 23:53:36", entity_id: nil, person_id: 14, company_id: 15>
ruby-1.9.2-p136 :033 > p.jobs.first.title
=> nil
ruby-1.9.2-p136 :034 > p.title
=> nil
ruby-1.9.2-p136 :035 > j
=> #<Job id: 18, type: "Job", created_at: "2011-01-24 23:53:36", updated_at: "2011-01-24 23:53:36", entity_id: nil, person_id: 14, company_id: 15>
ruby-1.9.2-p136 :036 > j.title
=> "dfkjld"
NOTE: The name class is abstracted and associated with Job. There is a helper module included with both classes like so.
def title
if class_name == "Job"
name.value
elsif class_name == "Person"
if jobs.empty? then "Unemployed" else jobs.first.title end
else
nil
end
end
ruby-1.9.2-p136 :015 > j.name
=> #<Name id: 16, kind: nil, value: "dfklj", name_id: nil, event_id: 19, instrument_id: nil, transaction_id: nil>
ruby-1.9.2-p136 :016 > j.name.value
=> "dfklj"
It’s probably a lazy loading issue.
p.jobs.firstisn’t pulling the ‘real’ data from your system, but using a memoized/cached version of it. And that cached version doesn’t contain a title.It could be solved by forcing
pto ‘reload its dependencies’. If p is an ActiveRecord object, there is areloadmethod:Another possibility is that j has local modifications, but these are not commited – for example, in ActiveRecord, if the title was initially nil and you set it on j without saving / reloading, it will not appear on
p.jobs.first:In order for that to work, you have to save to the database and reload p: