Attr_accessor nil on assocation

I’ll make this as simple as I can…

class MyClass < ActiveRecord::Base

attr_accessor :date

has_many :somethings

end

my_class = MyClass.find :first

my_class.date = Date.today
my_class.date # date object

my_class.somethings.first.my_class # my_class
my_class.somethings.first.my_class == my_class # true

my_class.somethings.first.my_class.date # nil

Any ideas?

On 28 Apr 2008, at 22:11, Daniel W. wrote:

my_class = MyClass.find :first

my_class.date = Date.today
my_class.date # date object

my_class.somethings.first.my_class # my_class
my_class.somethings.first.my_class == my_class # true

my_class.somethings.first.my_class.date # nil

With activerecord there can be more than one instance in memory
representing the same database object, and this is what happens here
(association ‘backlinks’ are not preloaded for you - there’s a fresh
database access). Since your date attribute isn’t persisted to the
database at all, this is to be expected unfortunately.

Fred

Frederick C. wrote:

With activerecord there can be more than one instance in memory
representing the same database object, and this is what happens here
(association ‘backlinks’ are not preloaded for you - there’s a fresh
database access). Since your date attribute isn’t persisted to the
database at all, this is to be expected unfortunately.

Fred

Gotcha. Now I can stop pulling my hair out and find another path.

Thanks, Fred.