【发布时间】:2020-10-20 19:51:48
【问题描述】:
我已经建立了以下关联:
class BookLaunch < ApplicationRecord
has_many :book_launch_locations, dependent: :destroy
has_many :stores, through: :book_launch_locations
....
class Store < ApplicationRecord
has_many :book_launch_locations
has_many :book_launch, through: :book_launch_locations
....
class BookLaunchLocation < ApplicationRecord
belongs_to :book_launch, :touch => true
belongs_to :store
end
BookLaunchLocation 有一个名为book_price 的属性。
create_table "book_launch_locations", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.integer "book_launch_id", null: false
t.integer "store_id", null: false
t.integer "book_price", default: 1, null: false
...
t.index ["book_launch_id", "store_id"], name: "pair_uniqueness", unique: true
t.index ["book_launch_id"], name: "index_book_launch_locations_on_book_launch_id"
t.index ["store_id"], name: "index_book_launch_locations_on_store_id"
end
我想将 book_price 添加到 BookLaunch 模型中的商店,以便当我调用
@book_launch.stores,它将具有商店的所有属性+book_price属性。
这可能吗?
我是这样使用 fastJsonApi 调用它的:
options = {
include: [:book_launch, :'book_launch.properties']}
json = Api::launchSummarySerializer.new(summaryData, options).serialized_json
render json: json
【问题讨论】:
标签: ruby-on-rails associations has-many-through rails-models