【发布时间】:2015-07-16 21:52:03
【问题描述】:
我需要在 Ruby on Rails 中编写一个由 4 个级别组成的模型关系:
服装店 -> 品牌 -> 服装品牌 -> 衬衫
如果你想以最有效的方式做到这一点,你会编写类似这样的伪代码吗?
shop< has_many :brands
attributes: name,url,etc
brand< belongs_to :shop
has_many :lines
attributes: name,owner,etc
line< belongs_to :brand
has_many :shirts
attributes: name,year,etc
shirt< belongs_to :line
attributes :name, price, color, etc...
或者是这样的:
shirt<
attributes : shop, shopName, shopUrl, brand, brandName, line, name, price, color, etc...
我的意思是,拥有更多具有更少属性的模型还是更少具有更多属性的模型更好?
【问题讨论】:
-
我更喜欢第一个选项。我见过一些公司把所有东西都塞到一个(1000 列长)表中——它运行得不是很好。保持模型小,这样它们可以更好地扩展。
标签: ruby-on-rails ruby-on-rails-4 model-view-controller