Ember polymorphic belongsTo - 无法分配相关对象

时间:2016-10-05 22:55:51

标签: ruby-on-rails-4 ember-data active-model-serializers json-api

我试图在Ember 2.7.1和Ember-Data 2.7.0中设置多态关联。从Rails Api获取数据绝对没有问题 - 使用带有JSONAPI的ActiveModelSerializers。

我尝试创建新的计算实例并将现有的采购订单分配到其上。看起来没问题,但在执行save()调用后,它失败并出现以下错误。

uncaught Error: Assertion Failed: You cannot add a record of type 'purchase-order' to the 'calculation.calculable' relationship (only 'calculable' allowed)

知道如何解决这个问题吗?

这是我的代码的相关部分。我有很多代码,但我希望你能对我的问题有所了解:/

应用程序/控制器/购买订单/ 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(' calculable',{polymorphic:true})应该存储一个购买订单或任何其他类的实例,因为它的多态?...

应用程序/模型/ calculation.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')
});

rails - app / controllers / computation_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

rails - app / models / 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

rails - app / serializers / calculation_serializer.rb

class CalculationSerializer < ActiveModel::Serializer
  attributes :id, :total_pallets, :total_purchase_positions
  belongs_to :calculable, polymorphic: true
end

rails - app / serializers / 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

我希望有人可以帮助我

干杯,

迈克尔

1 个答案:

答案 0 :(得分:0)

经过一天的搜索后找到了答案......看来你必须使用mixin。有关解决方案,请参阅https://stackoverflow.com/a/36272739/1021901