开发者

Rails Controller: Is it good practice to define controller for associations?

开发者 https://www.devze.com 2023-02-17 11:26 出处:网络
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

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.

0

精彩评论

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