HABTM 最佳实践
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
当然,多态性似乎是正确的想法。它正盯着你的脸。但如果你太过分了,我想警告你远离
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