Rail应用程序中双向多态关系的缺点是什么?

时间:2022-03-24 04:29:00

I'm building a Rail 3.2 app that has some complex data relationships. I'm wondering what is the best way to structure the database.

我正在构建一个具有一些复杂数据关系的Rail 3.2应用程序。我想知道构建数据库的最佳方法是什么。

The data I'm trying to model coud be compared to elements of Facebook.

我试图建模的数据可以与Facebook的元素进行比较。

For example, there are items that a user can create:

例如,用户可以创建项目:

  • comments
  • photos
  • posts
  • status
  • updates

Each of these items can belong to a

这些项目中的每一项都属于a

  • user
  • business
  • location

So for example "User A took a photo at MacDonalds in Paris".

例如,“用户A在巴黎麦克唐纳拍了一张照片”。

I want to show an activity stream on each user, business and location page.

我想在每个用户,业务和位置页面上显示活动流。

The way I've thought of doing this involves a two way polymorphic relationship.

我想到这样做的方式涉及双向多态关系。

one for activity:

一个活动:

class ActivityFeed
  belongs_to :feedable, :polymorphic => true
end

class Comment | Photo | Status
  has_many :activity_feeds, :as => :feedable
end

and one for ownership

和一个所有权

class ActivityOwner
  belongs_to :ownable, :polymorphic => true
end

class User | Business | Location
  has_many :activity_owners, :as => :ownable
end

Then activity_owner would belong to activity_feed, and activity_feed would have many activity_owners.

然后activity_owner属于activity_feed,而activity_feed将有许多activity_owner。

I hope that makes sense?

我希望这是有道理的?

Is this a good way to tackle this case? Are there any performance issues I should be thinking about? Is there a better way?

这是处理这种情况的好方法吗?我应该考虑哪些性能问题?有没有更好的办法?

A followup question: using this approach, is it possible to map the same model to a polymorphic relationship in multiple ways. For example a user could be an owner of a photo, and they could be a tagged user.

后续问题:使用这种方法,是否可以以多种方式将同一模型映射到多态关系。例如,用户可以是照片的所有者,并且他们可以是标记的用户。

I'm stil trying to learn Rails, and I'm struggling to get my head around this issue. I'd really appreciate opinions, experience and ideas from others, as well as any pointers to useful information or guides.

我很想学习Rails,我正在努力解决这个问题。我非常感谢来自他人的意见,经验和想法,以及任何有用信息或指南的指示。

Thanks!

2 个解决方案

#1


0  

http://blog.smartlogicsolutions.com/2008/06/13/ruby-on-rails-polymorphic-association-benchmarks/

it is old a little bit but still actuall

它有点旧,但仍然是实际的

no big drawbacks if u r using right indexes, just less columns in your db

如果您使用正确的索引,数据库中的列数较少,则没有大的缺点

#2


-1  

Interesting, Have a look at Polymorphic Association (revised) by Ryan Bates. I think it will be a perfect approach to what you are trying to do. Nice and clean too

有趣的是,看看Ryan Bates的Polymorphic Association(修订版)。我认为这将是您尝试做的完美方法。很好,也很干净

#1


0  

http://blog.smartlogicsolutions.com/2008/06/13/ruby-on-rails-polymorphic-association-benchmarks/

it is old a little bit but still actuall

它有点旧,但仍然是实际的

no big drawbacks if u r using right indexes, just less columns in your db

如果您使用正确的索引,数据库中的列数较少,则没有大的缺点

#2


-1  

Interesting, Have a look at Polymorphic Association (revised) by Ryan Bates. I think it will be a perfect approach to what you are trying to do. Nice and clean too

有趣的是,看看Ryan Bates的Polymorphic Association(修订版)。我认为这将是您尝试做的完美方法。很好,也很干净