开发者

How to do lookup table for roles

I have a table that lists authors;开发者_如何学编程

column :name, :string,
column :role_id, :integer

I have a table for the roles

column :role_name, :string

Models:

Author

belongs_to :role

Role

has_many :authors

This seems strange. Is it correct that Author has one role and a role belongs to many authors?

Now if I have an author I now i can do the following;

@author.name and get the name.

How do I get the role_name

@author.role.role_name?

Thank you in advance


Your associations are correct - look at this for explanation. Also, you answered your own question, getting the role name for an author can be done with

@author.role.role_name

Another option would be to add the following line to the Author model

delegate :role_name, :to => :role

and then you would be able to get the role name simply with

@author.role_name
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜