[英]How to model an owner and members with a has_many :through association?
In my rails application I have Event and User models. 在我的rails应用程序中,我有事件和用户模型。 The business rules dictate that an Event has one owner and many members, and the owner must be a member.
业务规则规定事件有一个所有者和许多成员,并且所有者必须是成员。 The inverse would be User having many 'owned events' and 'events' (events he/she is a member of but not the owner).
相反的是用户有许多“拥有的事件”和“事件”(事件他/她是所有者的成员但不是所有者)。 I had originally planned to model this association like so:
我本来打算像这样建模这个协会:
class Event < ActiveRecord::Base
has_many :event_members, dependent: :destroy
has_one :owner, through: :event_members, source: :user, conditions: { owner: true }
has_many :members, through: :event_members, source: :user
end
class EventMember < ActiveRecord::Base
belongs_to :event
belongs_to :user
end
class User < ActiveRecord::Base
has_many :owned_events, through: :event_members, conditions: { owner: true }
has_many :events, through: :event_members
end
I thought that this would ensure one owner and that owner would be included in the members association for free. 我认为这将确保一个所有者和所有者将免费包含在成员协会中。 However, this doesn't work since I can't specify a has_one through a collection - the :event_members association.
但是,这不起作用,因为我无法通过集合指定has_one - :event_members关联。
Is there a better way to model this? 有没有更好的方法来模拟这个? I could add a transient 'owner' attr on Event that queries EventMember by the owner flag - but will that work for setting the owner (ie can I grab the EventMember record that is created and make sure the owner flag is set)?
我可以在通过所有者标志查询EventMember的事件上添加一个瞬态“所有者”attr - 但这是否可以用于设置所有者(即我可以获取创建的EventMember记录并确保设置所有者标志)?
What about adding an owner
attribute on the join model EventMember
(also a field of type boolean in the event_members
table, with default value of false
). 如何在连接模型
EventMember
上添加owner
属性(在event_members
表中也是boolean类型的字段,默认值为false
)。 Access the owner
and owned_events
with methods on each respective model. 使用每个相应模型上的方法访问
owner
和owned_events
。
class Event < ActiveRecord::Base
has_many :event_members
has_many :members, :through => :event_members, :source => :user
def owner
event_members.ownerships.map(&:user).first
end
end
class EventMember < ActiveRecord::Base
belongs_to :event
belogns_to :user
scope :ownerships, -> { where(:owner => true) }
end
class User < ActiveRecord::Base
has_many :event_members
has_many :events, :through => :event_members
def owned_events
event_members.ownerships.map(&:event)
end
end
The Event
object may have the responsibility of making a user its owner. Event
对象可能有责任使用户成为其所有者。
class Event
def make_owner!(user)
membership = event_members.where(:user_id = user).first
membership.owner = true
membership.save
end
end
Maybe place logic in there to enforce only one owner and abstract part of make_owner!
也许在那里放置逻辑来强制只有一个所有者和
make_owner!
抽象部分make_owner!
to EventMember
depending on how messy this gets in your application. 到
EventMember
取决于你的应用程序有多乱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.