HABTM 最佳实践

发布于 2024-11-10 03:21:56 字数 721 浏览 0 评论 0原文

我有 2 个主要实体:UserProfile 和 Property。基本上,UserProfile 需要维护 3 个不同的 Properties 列表(注意,每个列表类型都会有附加属性)

是否有人认为以下设计有任何问题:

class UserProfile < ActiveRecord::Base
  has_many :shortlists
  has_many :booklists
  has_many :proplists
end

class Shortlist < ActiveRecord::Base
  has_and_belongs_to_many :properties
end

class Booklist < ActiveRecord::Base
  has_and_belongs_to_many :properties
end

class Proplist < ActiveRecord::Base
  has_and_belongs_to_many :properties
end

class Property < ActiveRecord::Base
  has_and_belongs_to_many :shortlists
  has_and_belongs_to_many :booklists
  has_and_belongs_to_many :proplists
end

我考虑的另一种方法是对 Property 实体使用多态性, 但不确定哪种方式更“铁轨方式”

I have 2 main entities, UserProfile and Property. Basically, the UserProfile needs to maintain 3 different lists of Properties (note, each list type will have additional properties)

Does anyone see anything wrong with the following design for doing so:

class UserProfile < ActiveRecord::Base
  has_many :shortlists
  has_many :booklists
  has_many :proplists
end

class Shortlist < ActiveRecord::Base
  has_and_belongs_to_many :properties
end

class Booklist < ActiveRecord::Base
  has_and_belongs_to_many :properties
end

class Proplist < ActiveRecord::Base
  has_and_belongs_to_many :properties
end

class Property < ActiveRecord::Base
  has_and_belongs_to_many :shortlists
  has_and_belongs_to_many :booklists
  has_and_belongs_to_many :proplists
end

The other way I was considering is to use polymorphism for the Property entity,
but not sure which way would be more 'the rails way'

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

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

发布评论

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

评论(2

诗酒趁年少 2024-11-17 03:21:56

HABTM 稍微过时了,已被 has_many :through 取代。此外,请查看Railscast on Polymorphic Associations。瑞安·贝茨(Ryan Bates)很好地解释了这一点。

HABTM is slightly out of date, and has been replaced with has_many :through. In addition check out the Railscast on Polymorphic Associations. Ryan Bates does an excellent job explaining this.

女皇必胜 2024-11-17 03:21:56

当然,多态性似乎是正确的想法。它正盯着你的脸。但如果你太过分了,我想警告你远离 has_many_polymorphs gem。过时、有缺陷,Rails 3 版本尚未接近成熟,并且它使您的开发环境变得极其繁重(每个请求需要额外的 4-6 秒来加载)。

多了解一下多态性,如下所示:

http:// Quickleft.com/blog/advanced-activerecord-recipes-doubly-polymorphic-join-models

Polymorphism seems like the right idea, of course. It's staring you right in the face. But if you go too far with it, I'd like to warn you away from the has_many_polymorphs gem. Out of date, buggy, the Rails 3 version isn't close to mature, and it makes your development environment extremely heavy (every request takes an extra 4-6 seconds to load).

Read up on polymorphism a bit more, like here:

http://quickleft.com/blog/advanced-activerecord-recipes-doubly-polymorphic-join-models

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