有模型定价:
pricingable_id:integer
pricingable_type:string
**store_id**:integer
cost_price:decimal
margin:integer
discount:integer
active:boolean还有其他的模型产品:
name
description
...
has_many :pricings, as: :pricingable那么,我如何委托属性cost_price、保证金、折扣、从产品到定价依赖于store_id (单一)?
尝试了一些类似于has_one :pricing,-> { where store_id: Store.current_id }之类的东西,但是没有道理。
UPD:我需要从产品模型中指定定价属性:
Store.current_id = 1
p = Product.new
p.cost_price = 100
p.margin = 10
p.discount = 5
p.active = true
p.save
pricing = Product.**pricing** (not pricings, one pricing depending to current store)
pricing.cost_price
=> 100发布于 2013-12-13 19:46:34
我建议改变你们的联系。
价格(与定价不同,在我看来是不直观的)应该与Product相关联。
在这个布局中,一切都非常简单,我认为这就是你想要实现的。
前:
Price.where(product_id: Product.first, store_id: 1).cost_price编辑例子。
比方说,在Costco、Walmart和Aldi三家商店里,都有一种有id 1的燕麦片产品。(ids 1.3)
如果你想在送礼店(比如说沃尔玛)改变这种产品的价格,你可以这样做
Price.where(store_id: 2, product_id: 1).update {}更多的例子。
给定产品的所有价格:
Product.first.joins(:prices)商店的所有价格(及相关价格):
Store.first.joins(products: :prices)发布于 2013-12-13 19:45:50
听起来你想要一个多态关联..。请查看本rails指南中的主题:
basics.html#polymorphic-associations
https://stackoverflow.com/questions/20574552
复制相似问题