Ruby on rails 如何设置“的关联”;选项“;模型

Ruby on rails 如何设置“的关联”;选项“;模型,ruby-on-rails,ruby,ruby-on-rails-3.1,rails-activerecord,Ruby On Rails,Ruby,Ruby On Rails 3.1,Rails Activerecord,如果已将型号商店、产品、用户和价格与以下关联 class User < ActiveRecord::Base has_many :products has_many :stores has_many :prices end class Store < ActiveRecord::Base belongs_to :user has_many :prices end class Product < ActiveRecord::Base belongs_t

如果已将型号
商店
产品
用户
价格
与以下关联

class User < ActiveRecord::Base
  has_many :products
  has_many :stores
  has_many :prices
end

class Store < ActiveRecord::Base
  belongs_to :user
  has_many :prices
end

class Product < ActiveRecord::Base
  belongs_to :user
  has_many :prices
end

class Price < ActiveRecord::Base
  belongs_to :user
  belongs_to :product
  belongs_to :store
end

class Estate < ActiveRecord::Base
  belongs_to :user
end
class用户
并希望创建一个
选项
模型,该模型包含特定于模型的选项类型,例如,如果某个地产有后院、游泳池、网球场或价格有交易,则提供折扣或买一送一。这是由多态关联完成的吗?
我这样做是为了不必为每个模型创建一个选项模型,只需为我要添加的所有新选项创建一个模型。这是正确的方法吗?

如果使用多态选项模型,那么对象所属的每个类/记录的字段都是相同的。我不认为这是你想要的,因为交易没有游泳池,房产也不是买一送一(我希望!)

我会考虑使用。这样,只需在模型中添加一个文本列(称为“选项”),然后就可以定义所需的所有选项

class Estate < ActiveRecord::Base
  belongs_to :user
  store :options, accessors: [ :backyard, :pool, :tennis, :butler, :wine_cellar ]
end

estate = Estate.new
estate.pool = true
estate.options[:number_of_sharks] = 3 # Attributes not defined as accessors work too
class Estate
Lol@the buy one get one free estate。如果我使用的是Rails 3.1,我是否需要制作一个
价格选项
地产选项
来做我想做的事情?好吧,你仍然可以对所有人使用多态选项模型,但你必须将所有选项放在那里,并为你的视图添加更多的逻辑(不推荐)。或者,您可以直接将选项放入模型/表中,但如果需要任意选项,则不建议这样做,否则这些选项会经常更改(大量迁移)。