views:

46

answers:

1

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

A: 

You don't have a relation between domain and role, this is the reason you're getting the error when trying to use user1.domains.first.role.

j.
I'm still struggling to get this working scoped by domain. I want each user to have a single distinct role within each domain that he is a member. Can you explain what kind of association I need to achieve this?
Ben Langfeld