Reputation: 637
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"
Upvotes: 2
Views: 244
Reputation: 52651
It's probably a lazy loading issue. p.jobs.first
isn'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 p
to 'reload its dependencies'. If p is an ActiveRecord object, there is a reload
method:
p.reload
p.jobs.first
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
:
> p.jobs.first.title
nil
> j = Jobs.find(1) # same job as p.jobs.first.title
...
> j.title = 'blah'
'blah'
> j.title
'blah'
> p.jobs.first.title
nil
In order for that to work, you have to save to the database and reload p:
> j.save
true
> p.reload
p.jobs.first.title
> 'blah'
Upvotes: 6