简体   繁体   English

如何使用has_many:通过关联为所有者和成员建模?

[英]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. 使用每个相应模型上的方法访问ownerowned_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.

相关问题 模型协会-has_many:through - Association for model - has_many :through 如何在has_many中获取模型的属性:通过Rails 5中的关联 - How to get attributes of model in has_many :through association in Rails 5 轨道如何通过关联使用has_many保存模型 - rails how to save a model with has_many through association 如何通过关联使用 has_many 自动更新模型? - How to automatically update model with has_many through association? 如何在has_many的中间模型中创建has_many关联:以最佳方式通过关联Rails - How to create has_many associations in the intermediate model of has_many :through association in an optimal way Rails rails和cancan - 如何通过关联限制公司向has_many中的用户所有者显示操作 - rails and cancan - how to restrict the company show action to users owner in has_many through association has_many:通过关联 - The has_many :through Association 如何通过在rails中通过关联对has_many进行关联来实现has_many? - How do I do a has_many through association on a has_many through association in rails? 如果涉及多态关联,如何创建“ has_many through”关联? - How to create a 'has_many through' association if a polymorphic association is involved? Rails 4 + Pundit:在has_many中加入模型授权:通过关联 - Rails 4 + Pundit : join model authorization in has_many :through association
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM