我正在为一个艺术展览网站构建一个rails应用程序。目前我有4个模型,馆长,展览,艺术家和艺术品。
应用程序的工作方式如下:一个展览可以由多个策展人策划,一个展览可以展示多个艺术作品,一个艺术作品可以在多个展览中展示,艺术家可以拥有多个艺术作品,并且艺术作品属于一个艺术家。
我是一个铁路新手,我很难建立模型之间的关系。你能告诉我我做得对吗,或者也许有更好的方法?
curator.rb
class Curator < ActiveRecord::Base
has_and_belongs_to_many :exhibitions
endexhibition.rb
class Exhibition < ActiveRecord::Base
has_and_belongs_to_many :curators
has_and_belongs_to_many :artworks
endartwork.rb
class Artwork < ActiveRecord::Base
has_and_belongs_to_many :exhibitions
belongs_to :artist
endartist.rb
class Artist < ActiveRecord::Base
has_many :artworks
end谢谢!
发布于 2013-02-08 18:05:59
如果你想对你的关系进行验证,使用回调或者想要添加额外的属性,我推荐使用has_many :Through.
如果你想了解更多关于这个问题的信息,这个rails指南是很棒的:
http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many
不过,您的HABTM看起来很好。如果您遇到任何问题,建议您也引用您的连接表。
发布于 2013-02-08 17:58:27
在我看来是对的..。创建连接表的命名约定为:
curators_exhibitions
artworks_exhibitions希望你能明白我的意思。
此外,如果您想要在关系之上进行验证,您还可以查看has_many :through!
https://stackoverflow.com/questions/14769845
复制相似问题