On May 20, 8:02 am, Manasi Vora <[email protected]>
wrote:
> Hi Brian,
> I think even if it is has_one: through or has_many :through, you should
> define -
> class User < ActiveRecord::Base
>    has_many :membership
>    has_one :role, :through => :membership
>  end
>
> when defining user and membership. Then try user.role

Thanks for the tip, Manasi.  This didn't solve the problem.  After
defining them as you suggest I do the following:
    user = User.new
    user.save!
    membership = user.memberships.create
    role = membership.create_role

And then in the debugger:
(rdb:1) user
#<User id: 1, created_at: "2009-05-20 17:34:08", updated_at:
"2009-05-20 17:34:0
8">
(rdb:1) user.memberships.find(:first)
#<Membership id: 996332878, user_id: 1, role_id: nil, created_at:
"2009-05-20 17
:34:08", updated_at: "2009-05-20 17:34:08">
(rdb:1) membership
#<Membership id: 996332878, user_id: 1, role_id: 1, created_at:
"2009-05-20 17:3
4:08", updated_at: "2009-05-20 17:34:08">
(rdb:1) membership.role
#<Role id: 1, created_at: "2009-05-20 17:34:08", updated_at:
"2009-05-20 17:34:0
8">
(rdb:1) user.role
nil

Now the interesting thing is that user.memberships.find(:first) has no
role_id, but membership does.  I'm not sure what this means, but I'll
explore it further.  I suspect my problem might be cached records,
but:

(rdb:1) user.memberships(true).find(:first)
#<Membership id: 996332878, user_id: 1, role_id: nil, created_at:
"2009-05-20 17
:34:08", updated_at: "2009-05-20 17:34:08">

I thought the 'true' caused this to be reloaded, so I don't have an
explanation for the discrepency.

I'm a little relieved this didn't work, since the combination of
has_many memberships and has_one role doesn't make sense to me.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to