# Has\_one :through and update\_attributes

**URL:** <https://www.ruby-forum.com/t/has-one-through-and-update-attributes/157925>\
**Category:** Rails\
**Created:** [January 16, 2009, 4:35pm UTC](https://www.ruby-forum.com/t/has-one-through-and-update-attributes/157925 "2009-01-16T16:35:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![throwern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@throwern](https://www.ruby-forum.com/u/throwern)\
**Post date:** [January 16, 2009, 4:35pm UTC](https://www.ruby-forum.com/t/has-one-through-and-update-attributes/157925/1 "2009-01-16T16:35:16Z")

</div>

I’m wondering about the difference between update\_attributes(:method)  
and class.method=

I have setup an example similar to the one here:  
[http://ryandaigle.com/articles/2008/3/24/what-s-new-in-edge-rails-has-one-through](http://ryandaigle.com/articles/2008/3/24/what-s-new-in-edge-rails-has-one-through)

The main difference is that I use a has\_one instead of has\_many for my  
has\_one :through relationship (has\_one :subscription)

the following code works:

u = User.create(:name=\>“test\_user”)  
=\> #\<User id: 10001, name: “test\_user”, created\_at: “2009-01-16  
14:59:45”, updated\_at: “2009-01-16 14:59:45”\>

u.magazine = Magazine.create(:name =\> “mag”)  
=\> #\<Magazine id: 10005, name: “mag”, created\_at: “2009-01-16  
14:59:48”, updated\_at: “2009-01-16 14:59:48”\>

u.subscription  
=\> #\<Subscription id: 10001, user\_id: 10001, magazine\_id: 10005,  
created\_at: “2009-01-16 14:59:48”, updated\_at: “2009-01-16 14:59:48”\>

However, if I try

u.update\_attributes(:magazine=\>m)  
the return is true yet u.subscription is nil

Can anybody explain why that is?

---

<div class="post-metadata">

**Author:** ![throwern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@throwern](https://www.ruby-forum.com/u/throwern)\
**Post date:** [January 16, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/has-one-through-and-update-attributes/157925/2 "2009-01-16T16:58:17Z")

</div>

throwern wrote:

> However, if I try
> 
> u.update\_attributes(:magazine=\>m)  
> the return is true yet u.subscription is nil
> 
> Can anybody explain why that is?

I bet you $10 that u.magazine is nil as well… magazine is not an  
attribute but a has\_x association (with through)

ilan
