【发布时间】:2023-03-27 01:34:02
【问题描述】:
我有一个包含餐厅和产品的 Rails 5 应用。一种产品 has_many 餐厅,一种餐厅有多种产品。我创建了一个数据透视表并创建了和 has_many_through 关系,因为我想使用数据透视表。
产品:
has_many :restaurant_products, dependent: :destroy
has_many :restaurants, through: :restaurant_products
餐厅:
has_many :restaurant_products, dependent: :destroy
has_many :products, through: :restaurant_products
由于每个餐厅都可以修改每种产品的价格,因此我在我的 restaurant_products 表中添加了一个 custom_price 列。
现在我可以获取某家餐厅的所有产品:
@restaurant.products
但是这样,当我列出产品时,我无法访问自定义价格。如何以某种方式包含或访问正确的数据透视记录以获取自定义价格?
【问题讨论】:
标签: ruby-on-rails activerecord ruby-on-rails-5