我有以下实体:

  • 用户
  • 公司
  • 组织

用户需要,以便能够增加用户、公司、组织和未来党的对象到他们的朋友列表。

我原来的想法涉及使用一个友好的对象有多态性关系的朋友像这样:

简化友谊的模式:

  • user_id
  • friendable_id
  • friendable_type
User
- has_many :businesses, :through => :friendships, :conditions => ['friendable_type=?', 'Business'], :source => :friendable

我的问题是红宝石在轨道上Email不支持has_many通过关系通过多态关系上连接表。

在结束我希望有一个单一的加入表能够迭代过去的朋友列表的所有种类型,某些类型等。如下:

john = User.find(5)
john.friendships.map {|friendship| friendship.friendable }
john.businesses (has_many :through scoped to only the businesses)
john.organizations (has_many :through scoped only to the organizations)

我认为,使用户、企业和组织的继承的通用方类,并使该协会指向基地方类,但是在下一个对象,导致了一堆垃圾场,这似乎只是肮脏的。

我想知道是怎么其他人会这样的情况下在那里你想要创造一个对多关系到类似的对象,通过共同参加表使用Email同时避免了这个错误::)

Cannot have a has_many :through association 'User#businesses' on the polymorphic object 'Friendship#friendable'.

任何咨询意见极大的赞赏。

谢谢!

有帮助吗?

解决方案 2

它出现的轨插件has_many_polymorphs'可以让我做什么我想要的。

http://github.com/fauna/has_many_polymorphs/tree/master

添加这一行来我的用户模型,给我的功能我想:

has_many_polymorphs :friendables, :from => [:businesses, :organizations], :through => :friendships

其他提示

不使用:通过使用:作为=>friendable而不是在多态has_many关系

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top