Forum: Ruby on Rails has_one :through and update_attributes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
throwern (Guest)
on 2009-01-16 17:35
(Received via mailing list)
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-ne...

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?
Ilan B. (Guest)
on 2009-01-16 17:58
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
This topic is locked and can not be replied to.