Rails nested association issue

Posted by Ben Langfeld on Stack Overflow See other posts from Stack Overflow or by Ben Langfeld
Published on 2010-05-03T15:36:30Z Indexed on 2010/12/25 1:54 UTC
Read the original article Hit count: 181

Filed under:
|

Ok, so I'm new to both Ruby and Rails and I'm trying to do what I believe is called a nested association (please correct me if this is the wrong terminology). I currently have a User model and a Domains model and I have many to many associations setup (using has_many :through) between the two, and this works fine.

I now want to extend this to allow for a single role per domain per user (eg User1 is a member of Domain1 and has the role "Admin"). I have setup a Roles model with a single field (name:string) and have created three roles. I have also added a role_id column to the join table (memberships). I expected (and this is probably the issue) to be able to just use

user1 = User.find(1)
user1.domains.first
=> <some domain object>
user1.domains.first.role
=> <some role object>

but this returns a method not defined error.

Can anyone tell me what I'm failing to grasp here?

My model classes can be seen at http://gist.github.com/388200

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about associations