ActiveRecord :: AssociationTypeMismatch,但为什么呢?

时间:2013-11-05 19:36:33

标签: ruby-on-rails ruby json activerecord ember.js

这不是同名的另一个问题的副本。

我正在使用Rails 4.0和ruby 2.0.0与Ember(以及Ember数据)。

我的JSON以以下格式发布到OrdersController的create操作:

Parameters: {"order"=>{"status"=>"pending payment", "date"=>"5/11/2013", ... "user_id"=>11, "booking_id"=>8}}

其中...是JSON中发送的其他属性的截断。

我得到上面的错误:返回Fixnum(整数)而不是用户关联记录。我的订单型号:

class Order < ActiveRecord::Base
    resourcify
    belongs_to :user
    belongs_to :booking, inverse_of: :orders
end

OrdersController的Rails 4强参数:

# Strong Parameters (Rails 4)
def order_params
  params.require(:order).permit(:status, :date, ... :user_id, :booking_id)
end

其中...再次截断其他属性。

我知道由于类型不匹配而导致错误,但我无法弄清楚原因。我有其他模型使用相同的语法来构建记录关联,并且没有一个返回类型不匹配。

如果您需要,请在OrdersController上执行create操作:

# POST /orders
# POST /orders.json
def create
  @order = Order.create(order_params)
  if @order.new_record?
    render json: { errors: @order.errors.messages }, status: 422
  else
    render json: @order, status: 201
  end
end

请帮忙!

编辑:完整错误,按要求(这是一个怪物,我正在使用Heroku):

ActiveRecord::AssociationTypeMismatch (User(#70045315577100) expected, got Fixnum(#70045289133740)):


vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:216:in 'raise_on_type_mismatch!'
   vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/associations/belongs_to_association.rb:11:in 'replace'
   vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/associations/singular_association.rb:17:in 'writer'
  vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/associations/builder/association.rb:78:in 'user='
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:92:in 'block in assign_attributes'
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:91:in 'each'
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:91:in 'assign_attributes'
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/inherited_resource.rb:10:in 'load_resource_instance'
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:32:in 'load_resource'
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:25:in 'load_and_authorize_resource'
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:10:in 'block in add_before_filter'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:397:in '_run__1255486436321517885__process_action__callbacks'
   vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in 'run_callbacks'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/abstract_controller/callbacks.rb:17:in 'process_action'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal/rescue.rb:29:in 'process_action'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal/instrumentation.rb:31:in 'block in process_action'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications.rb:159:in 'block in instrument'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in 'instrument'
   vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications.rb:159:in `instrument'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal/instrumentation.rb:30:in 'process_action'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal/params_wrapper.rb:245:in 'process_action'
   vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/railties/controller_runtime.rb:18:in 'process_action'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/abstract_controller/base.rb:136:in 'process'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/abstract_controller/rendering.rb:44:in 'process'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal.rb:195:in 'dispatch'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal/rack_delegation.rb:13:in 'dispatch'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal.rb:231:in 'block in action'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/routing/route_set.rb:80:in 'call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/routing/route_set.rb:80:in 'dispatch'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/routing/route_set.rb:48:in 'call'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/journey/router.rb:71:in 'block in call'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/journey/router.rb:59:in 'each'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/journey/router.rb:59:in 'call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/routing/route_set.rb:655:in 'call'
  vendor/bundle/ruby/2.0.0/gems/rack-pjax-0.7.0/lib/rack/pjax.rb:12:in 'call'
  vendor/bundle/ruby/2.0.0/gems/newrelic_rpm-3.6.8.168/lib/new_relic/rack/error_collector.rb:43:in 'call'
 vendor/bundle/ruby/2.0.0/gems/newrelic_rpm-3.6.8.168/lib/new_relic/rack/agent_hooks.rb:22:in 'call'
  vendor/bundle/ruby/2.0.0/gems/newrelic_rpm-3.6.8.168/lib/new_relic/rack/browser_monitoring.rb:16:in 'call'
  vendor/bundle/ruby/2.0.0/gems/rack-cors-0.2.8/lib/rack/cors.rb:54:in 'call'
   vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/manager.rb:35:in 'block in call'
   vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/manager.rb:34:in 'catch'
   vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/manager.rb:34:in 'call'
   vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/etag.rb:23:in 'call'
   vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/conditionalget.rb:35:in 'call'
   vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/head.rb:11:in 'call'
2   vendor/bundle/ruby/2.0.0/gems/remotipart-1.2.1/lib/remotipart/middleware.rb:27:in 'call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/params_parser.rb:27:in 'call'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/flash.rb:241:in 'call'
  vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/query_cache.rb:36:in 'call'
   vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in 'call'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/callbacks.rb:29:in 'block in call'
   vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in '_run__2306864034802654393__call__callbacks'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in 'run_callbacks'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/callbacks.rb:27:in 'call'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/remote_ip.rb:76:in 'call'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/debug_exceptions.rb:17:in 'call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/show_exceptions.rb:30:in 'call'
  vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/rack/logger.rb:38:in 'call_app'
  vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/rack/logger.rb:21:in 'block in call'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/tagged_logging.rb:67:in 'block in tagged'
 vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/tagged_logging.rb:25:in 'tagged'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/tagged_logging.rb:67:in 'tagged'
  vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/rack/logger.rb:21:in 'call'
 vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/request_id.rb:21:in 'call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/runtime.rb:17:in 'call'
   vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/cache/strategy/local_cache.rb:83:in 'call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/static.rb:64:in 'call'
 vendor/bundle/ruby/2.0.0/gems/heroku-deflater-0.5.1/lib/heroku-deflater/skip_binary.rb:19:in 'call'
 vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/deflater.rb:25:in 'call'
 vendor/bundle/ruby/2.0.0/gems/heroku-deflater-0.5.1/lib/heroku-deflater/serve_zipped_assets.rb:49:in 'call'
vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/engine.rb:511:in 'call'
 vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/application.rb:97:in 'call'
 vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:in 'method_missing'
 vendor/bundle/ruby/2.0.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:552:in 'process_client'
  vendor/bundle/ruby/2.0.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:632:in 'worker_loop'
 vendor/bundle/ruby/2.0.0/gems/newrelic_rpm-3.6.8.168/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb:22:in 'call'
  vendor/bundle/ruby/2.0.0/gems/newrelic_rpm-3.6.8.168/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb:22:in 'block (4 levels) in <top (required)>'
  vendor/bundle/ruby/2.0.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:500:in 'spawn_missing_workers'
  vendor/bundle/ruby/2.0.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:142:in 'start'
 vendor/bundle/ruby/2.0.0/gems/unicorn-4.6.3/bin/unicorn:126:in '<top (required)>'
  vendor/bundle/ruby/2.0.0/bin/unicorn:23:in 'load'
 vendor/bundle/ruby/2.0.0/bin/unicorn:23:in '<main>'

完整order_params

def order_params
  params.require(:order).permit(:status, :amount, :date, :user_id, :booking_id, :billing_surname, :billing_firstnames, :billing_address, :billing_city, :billing_postcode, :billing_country, :delivery_surname, :delivery_firstnames, :delivery_address, :delivery_city, :delivery_postcode, :delivery_country, :crypt)
end

完整订单create方法:

# POST /orders
# POST /orders.json
def create
  @order = Order.new(params[:order])

  respond_to do |format|
    if @order.save
      format.html { redirect_to @order, notice: 'Order was successfully created.' }
      format.json { render json: @order, status: :created }
    else
      format.html { render action: "new" }
      format.json { render json: @order.errors, status: :unprocessable_entity }
    end
  end
end

0 个答案:

没有答案