【问题标题】:TypeError: no implicit conversion of String into Integer on associationTypeError:关联时没有将字符串隐式转换为整数
【发布时间】:2014-09-15 09:10:47
【问题描述】:

我在第 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

【问题讨论】:

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


    【解决方案1】:

    您正在使用has_many :products, through: :order_products.so 而不是尝试Order.includes(:products).first 获取订单产品,您可以用作:-

    ##to get all products using order
    Order.first.products
    ##to get all order using products
    Product.first.orders
    

    因为你已经有一个共同的连接表order_products用于这个目的,所以使用它

    【讨论】:

    • 是的,但这只是一个例子。当我想迭代订单以获得例如产品价格,我得到同样的错误(@orders = Order.includes(:products).all 然后例如@order = @orders.sample; @order.products.sum(:price)。我想避免 n+1 问题。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多