简体   繁体   English

Rails,具有多个模型的has_many多态关联

[英]Rails, has_many polymorphic association to one model

I would like to have one model (event) that has multiple polymorphic HABTM associations to the same user model. 我想要一个模型(事件),该模型具有与同一用户模型的多个多态HABTM关联。

It should work something like this: 它应该像这样工作:

Event < ActiveRecord::Base 事件<A​​ctiveRecord :: Base
has_many :admin_users, :through => :event_users, :source => :userable, :source_type => 'User' has_many:admin_users,:through =>:event_users,:source =>:userable,:source_type =>'用户'
has_many :participating_users, :through => :event_users, :source => :userable, :source_type => 'User' has_many:participating_users,:through =>:event_users,:source =>:userable,:source_type =>'用户'
has_many :paid_users, :through => :event_users, :source => :userable, :source_type => 'User' has_many:paid_users,:through =>:event_users,:source =>:userable,:source_type =>'用户'
has_many :done_users, :through => :event_users, :source => :userable, :source_type => 'User' has_many:done_users,:through =>:event_users,:source =>:userable,:source_type =>'用户'
end 结束

class EventUser < ActiveRecord::Base 类EventUser <ActiveRecord :: Base
belongs_to :userable, :polymorphic => true 纯属_to:userable,:polymorphic => true
belongs_to :event 相关活动
end 结束

User < ActiveRecord::Bas 用户<ActiveRecord :: Bas
has_many :event_users, :as => :userable has_many:event_users,:as =>:userable
has_many :events, :through => :event_users has_many:events,:through =>:event_users
end 结束

This almost works!! 这几乎可以工作! The problem is though that userable_type gets the type "User" for all diffrent associations. 问题是,尽管userable_type为所有不同的关联获取类型“ User”。 Is it possible to solve this? 有可能解决这个问题吗?

I'm afraid your associations look totally wrong. 恐怕您的协会看起来完全错了。 First of all, if you have a 'has_many' on one side of a association, you have to have a 'belongs_to' on the other side. 首先,如果关联的一侧有一个“ has_many”,则另一侧必须有一个“ belongs_to”。 Secondly, I'm guessing done_user, admin_user etc inherit from User. 其次,我猜到done_user,admin_user等是从User继承的。 Am i correct ? 我对么 ?

And how different are participating_user, admin_user etc different from each other? ,participation_user,admin_user等彼此之间有何不同? Do you really need classes for each of these, can you make do with named scopes? 您是否真的需要每个类的类,您可以使用命名范围吗? I would suggest you simplify your data model. 我建议您简化数据模型。 Right now your modeling looks fuzzy. 现在,您的建模看起来很模糊。 Please do elaborate. 请详细说明。

EDIT 编辑

Honestly, I think your modeling is over complicated. 老实说,我认为您的建模过于复杂。 If I were you, given your descriptions of *_users, I would just have named scopes to retrieve that type of an user. 如果我是您,给定您对* _users的描述,我将只具有命名范围来检索该类型的用户。 So in effect 所以实际上

class Event < ActiveRecord::Base
  has_many :event_users
  has_many :users, :through => :event_users
end

class User < ActiveRecord::Base
  has_many :event_users
  has_many :events, :through => :event_users
end

So now, write named scopes in your User model to retrieve *_user. 因此,现在,在您的User模型中编写命名作用域以检索* _user。 Here's an excellent screencast on named_scopes . 这是named_scopes的精彩视频。

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

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