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

马尔辛·亚当奇克

我在第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
马尔辛·亚当奇克

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

没有将String隐式转换为Integer(TypeError)

+:没有将Integer隐式转换为String(TypeError)

TypeError:没有将Hash隐式转换为Integer

ruby-json没有将String隐式转换为Integer(TypeError)

没有将Hash隐式转换为String(TypeError)

Ruby将String隐式转换为Integer(typeError)

TypeError没有将(我的班级名称):: ActiveRecord_Relation隐式转换为Integer

为什么会出现TypeError:没有将符号隐式转换为Integer?

没有将ActiveSupport :: TimeWithZone隐式转换为Integer

没有将ActiveSupport :: SafeBuffer隐式转换为Integer

没有将Symbol隐式转换为Integer吗?

使用没有 => 的哈希时,没有将 nil 隐式转换为 String

JSON在Ruby中没有将String隐式转换为Integer

Ruby on Rails:`没有将String隐式转换为Integer以获取json字段

ActionView :: Template :: Error(没有将String隐式转换为Integer)

Chef属性“没有将String隐式转换为Integer”

TypeError:没有使用gsub将nil隐式转换为String

TypeError:没有将Capybara :: ElementNotFound隐式转换为String

Ruby-TypeError:没有将Hash隐式转换为String

TypeError(没有将nil隐式转换为String),仅在生产中

Ruby错误-'+':没有将Fixnum隐式转换为String(TypeError)

没有将StringIO隐式转换为String(TypeError)-ruby

没有将HTTParty :: Response隐式转换为String

没有将Array隐式转换为String吗?

没有将nil隐式转换为String错误

没有将 ActiveSupport::HashWithIndifferentAccess 隐式转换为 String?

eRuby - 在 if 语句中比较数字时没有将 nil 隐式转换为 String

创建新记录时,我没有将 Symbol 隐式转换为 String

Ruby on Rails:尝试从API获取JSON数据并将其保存到Postgres数据库-耙中止了!TypeError:没有将String隐式转换为Integer