开发者

Rails nested association issue

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 ta开发者_运维技巧ble (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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜