Ember 2.7.1とEmber-Data 2.7.0で多態的な関連付けを設定しようとしています。私は、Rails APIからデータを取得する際に、全く問題はありません - JSONAPIでActiveModelSerializersを使用しています。Ember多型belongsTo - 関連オブジェクトを割り当てることができません
私は、新しい計算インスタンスを作成して既存の注文を割り当てようとしました。 okと思われますが、save()を呼び出した後、以下のエラーで失敗します。
uncaught Error: Assertion Failed: You cannot add a record of type 'purchase-order' to the 'calculation.calculable' relationship (only 'calculable' allowed)
この問題を解決するにはどうすればよいですか?
ここに私のコードの関連部分があります。多くのコードI'tsが、私はあなたが私の問題についての概要を取得することを願って:/
アプリ/コントローラ/発注書/ edit.js
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
save() {
let bla = this.get('store').createRecord('calculation', {
calculable: this.get('model') // instance of purchase-order
});
bla.save();
}
}
});
アプリ/モデル/ calculable.js
import DS from 'ember-data';
export default DS.Model.extend({
totalPallets: DS.attr(),
totalPurchasePositions: DS.attr(),
calculable: DS.belongsTo('calculable', { polymorphic: true })
});
計算可能:DS.belongsTo( 'に計算'、{多型:真})?それは多型だから発注書または他の任意のクラスのインスタンスを格納する必要があり...
APP /モデル/計算.js
import DS from 'ember-data';
import Calculable from './calculable';
export default Calculable.extend({
});
アプリ/モデル/購買order.js
import DS from 'ember-data';
export default DS.Model.extend({
baanId: DS.attr(),
createdAt: DS.attr(),
updatedAt: DS.attr(),
deliveryDate: DS.attr(),
purchasePositions: DS.hasMany('purchase-position'),
calculation: DS.belongsTo('calculation'),
pallets: DS.hasMany('pallet')
});
レール - アプリ/コントローラ/ calculations_controller.rb
class CalculationsController < ApplicationController
def create
@result = Calculation.new(prms)
# if @result.save
# render json: @result
# end
render json: @result
end
private
def prms
pa = ActiveModelSerializers::Deserialization.jsonapi_parse(params, polymorphic: [:calculable])
# params.require(data: :attributes).permit(:name)
return pa
end
end
レール - アプリ/モデル/ calculation.rb
class Calculation < ActiveRecord::Base
belongs_to :calculable, polymorphic: true
end
rails-app/models/purchase_order.rb
class PurchaseOrder < ActiveRecord::Base
has_one :calculation, as: :calculable, dependent: :destroy
end
レール - アプリ/シリアライザ/ calculation_serializer.rb
class CalculationSerializer < ActiveModel::Serializer
attributes :id, :total_pallets, :total_purchase_positions
belongs_to :calculable, polymorphic: true
end
レール - アプリ/シリアライザ/ purchase_order_serializer.rb
class PurchaseOrderSerializer < ActiveModel::Serializer
attributes :id, :baan_id, :created_at, :updated_at, :delivery_date
has_one :calculation
end
デシベル/ schema.rb
create_table "calculations", force: :cascade do |t|
t.integer "total_pallets", limit: 4, default: 0
t.integer "total_purchase_positions", limit: 4, default: 0
t.integer "calculable_id", limit: 4
t.string "calculable_type", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "purchase_orders", force: :cascade do |t|
t.integer "customer_id", limit: 4
t.string "baan_id", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
t.integer "shipping_route_id", limit: 4
t.date "delivery_date"
t.boolean "delivered", default: false
t.integer "shipping_address_id", limit: 4
t.integer "warehouse_number", limit: 4, default: 0
t.integer "category_id", limit: 4
t.integer "priority_level", limit: 4, default: 1
t.boolean "cancelled", default: false
end
私は誰かができることを願っていますELP私
乾杯、
マイケル