# Attr\_accessor nil on assocation

**URL:** <https://www.ruby-forum.com/t/attr-accessor-nil-on-assocation/136104>\
**Category:** Rails\
**Created:** [April 28, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/attr-accessor-nil-on-assocation/136104 "2008-04-28T23:11:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [April 28, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/attr-accessor-nil-on-assocation/136104/1 "2008-04-28T23:11:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [April 28, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/attr-accessor-nil-on-assocation/136104/2 "2008-04-28T23:32:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [April 28, 2008, 11:49pm UTC](https://www.ruby-forum.com/t/attr-accessor-nil-on-assocation/136104/3 "2008-04-28T23:49:09Z")

</div>

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.
