开发者

Rails nested association issue

开发者 https://www.devze.com 2022-12-29 04:07 出处:网络
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 Dom

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

精彩评论

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

关注公众号