many to many table with an extra column in Rails

强颜欢笑 提交于 2019-12-05 02:58:44

问题


Hi guys! Is possible to do this with only two Rails models, User and Event:

Users
|id        |name         |age         |
|1         |danilo       |26          |
|2         |joe          |23          |
|3         |carlos       |50          |
|4         |katy         |45          |

Events_Users
|event_id     |user_id        |confirmed       |
|1            |1              |1               |
|3            |3              |0               |
|4            |3              |1               |
|2            |3              |1               |

Events
|id           |name                     |date            |
|1            |the end of the year      |31/12/2012      |
|2            |the end of the world     |21/12/2012      |
|3            |Party                    |18/12/2012      |
|4            |Dinner                   |19/12/2012      |

The problem is, the user can confirm or not their presence in an event, for this I used the table Events_Users, column confirmed (1 for confirmed). How can I do this with Rails ActiveRecord without an model "Event_User"? how can I manipulate the confirmed column in the User model?

I'm using Rails 3.2.9


回答1:


User and Event have many-to-many relationship, you can not set up this association with just 2 model, you must have the join model, or join table.

In your case, you added attribute confirmed, so you will need a join model, named Confirmation (as other people recommended). Your define associations will be like this:

class User
  has_many :events, through: :confirmations
  has_many :confirmations
end

class Event
  has_many :users, through: :confirmations
  has_many :confirmations
end

class Confirmation
  belongs_to :user
  belongs_to :event
end



回答2:


Instead of use for the User model, with the relation

has_and_belongs_to_many :events

and modify the join table Events_Users (it's a bit dirty)

is better to use a model Confirmation with two belongs_to relations:

belongs_to :user
belongs_to :event

I hope this can help you, Alessandro




回答3:


Since you have the extra field on the join table you'll want a join model. Check this out:

class User
  has_many :invitations
  has_many :invited_events,   -> {where(confirmed: false)}, class_name: 'Event', through: :invitations
  has_many :confirmed_events, -> {where(confirmed: true)},  class_name: 'Event', through: :invitations
end

class Event
  has_many :invitations
  has_many :invited_users,   -> {where(confirmed: false)}, class_name: 'User', through: :invitations
  has_many :confirmed_users, -> {where(confirmed: true)},  class_name: 'User', through: :invitations
end

class Invitation
  belongs_to :user
  belongs_to :event
end

With this, user.confirmed_events will provide the user's events only where the confirmed flag is set to true in the join table.



来源:https://stackoverflow.com/questions/13792436/many-to-many-table-with-an-extra-column-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!