简体   繁体   English

数据库关联,类has_many和belonge_to可以属于同一对象吗?

[英]Database Association, Can a class has_many and belong_to the same object?

So I'm trying to create a simple events web application where class User can both create class Event and participate in them. 因此,我正在尝试创建一个简单的事件Web应用程序,其中类User可以创建Event类并参与其中。 In the case class User creates a class Event, I want that event to belong_to the user. 在类User创建一个Event类的情况下,我希望该事件属于该用户。 But in case that same User signs up to participate in another event, I want the user to belong_to the event, and that event to has_many Users. 但是,如果同一用户注册参加另一个事件,我希望该用户属于该事件,并且该事件属于has_many Users。 I don't know how this would actually be written in the models though. 我不知道如何将其实际写入模型中。

In one case class User is a participant belonging to the event, and in the other they are the host and the event belongs_to them. 在一种情况下,类User是属于该事件的参与者,而在另一种情况下,类是主持人,而事件属于它们。 I wan't to keep this dry and not create separate classes of users(host and participant in this case) if I don't have too. 我也不想干,如果没有的话,也不要创建单独的用户类别(在这种情况下为主持人和参与者)。 A mere joint table doesn't seem to account for this conditional association. 单纯的联合表似乎并不能说明这种条件关联。 Any advice on the best way to do this? 关于最佳方法的任何建议吗?

class User ??? 类用户???

class Event ???? 班级活动????

Thanks! 谢谢!

You'll need to make 2 associations - one to link to created Events, and one to link to subscribed Events: 您需要建立2个关联-一个关联到创建的事件,另一个关联到订阅的事件:

create_table :event_subscriptions do |t|
  t.references :subscribed_user
  t.references :subscribed_event
end

create_table :events do |t|
  t.references :user
  # other fields
end

class User < ActiveRecord::Base
  has_many :events               # created Events
  has_many :event_subscriptions, :foreign_key => :subscribed_user_id
  has_many :subscribed_events, :through => :event_subscriptions  # subscribed Events
end

class EventSubscription < ActiveRecord::Base
  belongs_to :subscribed_user, :class_name => 'User'
  belongs_to :subscribed_event, :class_name => 'Event'
end

class Event < ActiveRecord::Base
  belongs_to :user               # the creating User
  has_many :event_subscriptions, :foreign_key => :subscribed_event_id
  has_many :subscribed_users, :through => :event_subscriptions   # subscribed Users
end

I would recommend you use the has_and_belongs_to_many association (documentation here ). 我建议您使用has_and_belongs_to_many关联( 此处的文档)。 Based on what you're trying to accomplish I don't think a joint table is avoidable. 基于您要完成的工作,我认为联合表是无法避免的。 Granted, that shouldn't discourage you! 当然,那不应该让你气!!

I would add a "role" column of type string to the Users table (ie Host or Participant) and use the has_and_belongs_to_many association. 我将类型string的“角色”列添加到用户表(即主持人或参与者),并使用has_and_belongs_to_many关联。 Note, you will need to create a joint table users_events that contains :user_id and :event_id columns. 注意,您将需要创建包含:user_id:event_id列的联合表users_events

Then, you can have a User that has the role of, let's say "host", create an Event and invite other users of roles "participant". 然后,您可以拥有一个具有以下角色的用户,即“主持人”,创建一个事件并邀请其他角色为“参与者”的用户。

Of course, this is just my suggestion. 当然,这只是我的建议。 There are several ways you can go about this. 有几种方法可以解决此问题。

Be aware that if an event has_many :users then the user table must have a event_id column, and a user can only belong to one event at a time. 请注意,如果一个事件has_many :users则用户表必须具有一个event_id列,并且一个用户一次只能属于一个事件。 I suspect you want a many to many relationship between users and events. 我怀疑您希望用户和事件之间存在多对多关系。 If that is the case you would use has_and_belongs_to_many :users association documented here 如果是这种情况,您可以使用has_and_belongs_to_many :users 这里记录的has_and_belongs_to_many :users关联

That said, yes, you can have multiple relations on the same classes. 也就是说,是的,您可以在同一个类上具有多个关系。

class Event < ActiveRecord::Base
  has_many :users
  belongs_to :host, :class_name => 'User'
end

The events table would have a column host_id , but event.host would still return a User. 该事件表将有一列host_id ,但仍event.host将返回用户。

I would handle that with a has_many and belong_to between User and Event. 我将在用户和事件之间使用has_many和belong_to进行处理。 User has_many Events and Event belongs_to User. 用户has_many事件和事件归属用户。

The rest would be your logic. 剩下的就是你的逻辑。 Do you want your has_many relationship to be your event owners, or your participants. 您是否希望has_many关系成为您的活动所有者或参与者? I'll assume the has_many relates to Event owners. 我假设has_many与事件所有者有关。

From there create a column called participants in your Event table. 从那里在事件表中创建一列称为参与者的列。 In the Event or User model you can then create a before_save method that inserts the user_id of your participant. 然后,在事件或用户模型中,您可以创建一个before_save方法,该方法插入参与者的user_id。

You could then create a scope in either model that returns a list of all participants of the Event. 然后,您可以在任一模型中创建一个范围,该范围返回事件的所有参与者的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM