Suppose i have mapping table map_user_roles. I have defined models Role and User. Both are associated to each other by relationship has_and_belongs_to_many. Of course it does not make sense to define model for mapping table in rails. I have defined users_controller and roles_controller for crud operations on user and role respectively.
For association of user-role, what should i do? Should i define separate controller like user_roles_co开发者_StackOverflow中文版ntroller or should i make modifications in Role and User controller(if so how to do so) ? Please suggest, what is good practice. Examples and good links would be great help Thanks for devoting time.
I don't see what a separate controller for the association would offer that couldn't be achieved with your existing UsersController
and RolesController
. Also, note that sometimes is does make sense to define a model for the mapping table, that's what the has_many :through
association is for. You should use it if you need to store extra attributes against the join model.
精彩评论