Search code examples
ruby-on-railsrubyactiverecordruby-on-rails-4postgresql-9.3

TypeError: no implicit conversion of String into Integer on association


I'm struggling 3rd day with this problem, which is common, but I can't solve it. I have 3 models:

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

When I'm running Order.includes(:products).first, I get this response:

[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'

This is my 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

Solution

  • Okay, I'm an idiot. I had method in Order class which was called hash, which is reserved for objects. It was calling other method, which was returning String type.

    Anyway, thanks for help.