Rails,命名约定
如果我有一个名为 events 的模型和一个名为 users 的模型,那么通过表/模型命名 has_many 的约定是什么?
是表的 events_users 吗?如果是这样,我在命名模型时遇到问题,因为rails似乎不喜欢EventUser,也不喜欢EventsUser......
What is the convention for naming a has_many through table/model if I have a model called events and one called users?
Is it events_users for the table? If so I get a problem with naming the Model, because rails doesn't seem to like EventUser, nor does it seem to like EventsUser...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有很多方法可以做到这一点。尝试将表命名为
events_users
。默认情况下,has_many through
或has_and_belongs_to_many
中涉及的两个表是复数的,并按字母顺序排序。如果您打算使用关联,则只需要一个模型。如果是,请按逻辑命名,并告诉 ActiveRecord 表名称为
events_users
。There are many ways to do this. Try naming your table
events_users
. By default, the two involved tables in ahas_many through
or ahas_and_belongs_to_many
are pluralized and ordered alphabetically.You only need a model for the association if you plan on using it. If so, name it logically, and tell ActiveRecord the table name is
events_users
.使用 has_many :through,你可以随意命名它 - Rails 根本没有问题...表名称始终是完全复数(userS,eventS,userS_eventS),并且模型始终是单数(user,event,user_event)
只需使用生成器:
With has_many :through, you name it whatever you want - Rails doesn't have a problem at all... TABLE names are always fully plural (userS, eventS, usersS_eventS), and models are always singular (user, event, user_event)
Just use the generator: