首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError:关联时没有将字符串隐式转换为整数

TypeError:关联时没有将字符串隐式转换为整数
EN

Stack Overflow用户
提问于 2014-09-15 09:10:47
回答 2查看 1.7K关注 0票数 0

我第三天一直在努力解决这个问题,这是很常见的,但我无法解决。我有三个模特:

代码语言:javascript
运行
复制
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时,我会得到以下响应:

代码语言:javascript
运行
复制
[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

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-15 10:14:13

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

不管怎样,谢谢你的帮助。

票数 0
EN

Stack Overflow用户

发布于 2014-09-15 09:38:33

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

代码语言:javascript
运行
复制
##to get all products using order
Order.first.products
##to get all order using products
Product.first.orders

因为已经有了用于此目的的公共联接表order_products,所以请使用它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25844495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档