TypeError:关联时没有将String隐式转换为Integer

时间:2014-09-15 09:10:48

标签: ruby-on-rails ruby activerecord ruby-on-rails-4 postgresql-9.3

我在第3天遇到这个问题,这很常见,但我无法解决。我有3个型号:

class Order < ActiveRecord::Base
  attr_accessor :subscription_length, :selected_products, :token, :ip

  delegate :name, :id, to: :client, prefix: true

  belongs_to :client
  has_many :order_products
  has_many :products, through: :order_products

  (...)
end

class OrderProduct < ActiveRecord::Base
  delegate :name, to: :product

  belongs_to :product
  belongs_to :order
end

class Product < ActiveRecord::Base
  include ActionView::Helpers::TextHelper

  belongs_to :shopkeeper
  has_many :order_products
  has_many :orders, through: :order_products

  (...)
end

当我正在运行Order.includes(:products).first时,我收到了此回复:

[50] pry(main)> Order.includes(:products).first
Order Load (0.8ms)  SELECT  "orders".* FROM "orders"   ORDER BY "orders"."id" ASC LIMIT 1
Product Exists (0.5ms)  SELECT  1 AS one FROM "products" INNER JOIN "order_products" ON "products"."id" = "order_products"."product_id" WHERE "order_products"."order_id" = $1 LIMIT 1  [["order_id", 1]]
OrderProduct Load (0.3ms)  SELECT "order_products".* FROM "order_products"  WHERE "order_products"."order_id" = $1  [["order_id", 1]]
Product Load (0.4ms)  SELECT "products".* FROM "products"  WHERE "products"."id" IN (5, 4)
TypeError: no implicit conversion of String into Integer
from /usr/local/var/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader/association.rb:77:in `block in associated_records_by_owner'

这是我的schema.rb

create_table "order_products", force: true do |t|
  t.integer "product_id",             null: false
  t.integer "order_id",               null: false
  t.integer "amount",     default: 0, null: false
end

add_index "order_products", ["order_id"], name: "index_order_products_on_order_id", using: :btree
add_index "order_products", ["product_id", "order_id"], name: "index_order_products_on_product_id_and_order_id", using: :btree
add_index "order_products", ["product_id"], name: "index_order_products_on_product_id", using: :btree

create_table "orders", force: true do |t|
  t.integer  "client_id",                 null: false
  t.string   "zip_code"
  t.string   "city"
  t.datetime "billing_from"
  t.datetime "billing_to"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "address"
  t.json     "config",       default: {}, null: false
end

add_index "orders", ["client_id"], name: "index_orders_on_client_id", using: :btree

create_table "products", force: true do |t|
  t.string   "name"
  t.text     "description"
  t.string   "image"
  t.float    "price"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "shopkeeper_id",                 null: false
  t.boolean  "disabled",      default: false, null: false
end

add_index "products", ["shopkeeper_id"], name: "index_products_on_shopkeeper_id", using: :btree

2 个答案:

答案 0 :(得分:0)

您正在使用has_many :products, through: :order_products。因此,您可以使用以下内容作为:

,而不是尝试Order.includes(:products).first 来获取订单的产品
##to get all products using order
Order.first.products
##to get all order using products
Product.first.orders

因为您已经为此目的使用了公共连接表order_products,所以请使用它

答案 1 :(得分:0)

好吧,我是个白痴。 我在Order类中有一个名为hash的方法,它是为对象保留的。它正在调用其他方法,它返回String类型。

无论如何,谢谢你的帮助。