建立用户“友谊”模型的最佳方式 与不同的实体?

发布于 2024-08-01 21:30:58 字数 1087 浏览 4 评论 0原文

我有以下实体:

  • 用户
  • 、公司
  • 、组织

用户需要能够将用户、公司、组织和未来的聚会对象添加到他们的朋友列表中。

我最初的想法涉及使用与朋友具有多态关系的 Friendship 对象,如下所示:

的 Friendship 模式:

  • user_idfriendable_idfriendable_type
  • 简化
  • 我的
User
- has_many :businesses, :through => :friendships, :conditions => ['friendable_type=?', 'Business'], :source => :friendable

问题是 Ruby on Rails ActiveRecord 不支持通过连接表上的多态关系来实现 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)

我考虑过让用户、业务和组织继承自通用 Party 类并使关联指向基 Party 类,但在 ORM 的上下文中会导致一堆垃圾字段,而且看起来很脏。

我想知道的是其他人将如何处理这样的情况,您希望使用 ActiveRecord 通过公共联接表创建与相似对象的一对多关系,同时避免此错误::)

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

非常感谢任何建议。

谢谢!

I have the following entities:

  • User
  • Company
  • Organization

Users need to be able to add Users, Companies, Organizations, and future party objects to their friend list.

My original idea involved using a Friendship object with a polymorphic relationship to the friend like so:

Simplified Friendship schema:

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

My problem is that Ruby on Rails ActiveRecord does not support has_many through relationships through polymorphic relationships on the join table such.

In the end I want to have a single join table to be able to iterate through to get a list of friends of all types, certain types, etc. like below:

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)

I've considered making the User, Business, and Organization inherit from a generic Party class and making the association point to the base Party class, but in the context of an ORM that leads to a bunch of junk fields and it just seems dirty.

What I'd like to know is how others would approach a situation like this where you want to create one-to-many relationships to similar objects through a common join table using ActiveRecord while avoiding this error: :)

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

Any advice greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

憧憬巴黎街头的黎明 2024-08-08 21:30:58

在多态 has_many 关系中不要使用 :through,而是使用 :as=>friendable

Do not use :through, use :as=>friendable instead in polymorphic has_many relationship

年华零落成诗 2024-08-08 21:30:58

看来 Rails 插件“has_many_polymorphs”允许我完全按照我的意愿去做。

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

将此行添加到我的用户模型中为我提供了我想要的功能:

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

It appears that the Rails plugin 'has_many_polymorphs' allows me to do exactly what I want.

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

Adding this line to my User model gave me the functionality I wanted:

has_many_polymorphs :friendables, :from => [:businesses, :organizations], :through => :friendships
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文