开发者

Is has_and_belongs_to_many the appropriate association here?

In my rails app, each User can create many Competitions and each Competition can have many Users entering the competition. I am wondering whether using has_and_belongs_to_many is appropriate because some Users will not create Competitions 开发者_如何转开发but will enter Competitions?

Is there an alternative approach that would be better to use?


For your case you will need a table with reference of user and competition (user_id and competition_id).

If you want some properties on each association between user and competition, (eg. same competition for different user has different rules or attributes ) then you will have to create a separate model(each association between user and competition will be an instance of that model) and you will need to use has_many :through else has_and_belongs_to_many is fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜