Rails 7.0中的`Object.group`会导致”Object doesn’t support #inspect”错误。

huangapple go评论59阅读模式
英文:

Rails 7.0 `Object.group` delivers Object doesn't support #inspect

问题

I've got orders which can be made from different suppliers. I want to group the total amount of money paid by the said supplier.

My SupplierOrder.rb:

class SupplierOrder < ApplicationRecord
  include Order

  belongs_to :product
  has_many :payments, as: :payable, dependent: :destroy

  accepts_nested_attributes_for :payments, allow_destroy: true
end

Now, a simple SupplierOrder.group(:supplier) in the console delivers me:

SupplierOrder.group(:supplier)
  SupplierOrder Load (0.5ms)  SELECT "supplier_orders".* FROM "supplier_orders" GROUP BY "supplier_orders"."supplier"
(Object doesn't support #inspect)
=>

If it's helpful, here my schema.rb:

create_table "supplier_orders", force: :cascade do |t|
  t.float "paid"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.float "price"
  t.string "supplier"
  t.string "order_number"
  t.integer "amount"
  t.bigint "product_id"
  t.index ["product_id"], name: "index_supplier_orders_on_product_id"
end

Here is my order.rb Concern (models/concerns/order.rb):

module Order
  extend ActiveSupport::Concern

  def full_title
    self.product.full_title
  end
end

Right now I'm getting supplier-Value from a global variable, defined in helpers/application_helper.rb:

module ApplicationHelper
  SIZES = %w(1:1 1:2 1:3 1:4 1:5 1:6 1:7 1:8)
  VERSIONS = %w(regular deluxe exclusive)
  COLORS = %w(black blue white)
  SUPPLIERS = %w(A B C D)
end

I don't override any initializers like in linked topics or do anything extraordinary. All (at least for now) other methods do work in the console, but just this one is not.

英文:

I've got orders which can be made from different suppliers. I want to group the total amount of money paid by the said supplier.

My SupplierOrder.rb:

class SupplierOrder < ApplicationRecord
  include Order

  belongs_to :product
  has_many :payments, as: :payable, dependent: :destroy

  accepts_nested_attributes_for :payments, allow_destroy: true
end

Now, a simple SupplierOrder.group(:supplier) in the console delivers me:

SupplierOrder.group(:supplier)
  SupplierOrder Load (0.5ms)  SELECT "supplier_orders".* FROM "supplier_orders" GROUP BY "supplier_orders"."supplier"
(Object doesn't support #inspect)
 =>

If it's helpful, here my schema.rb:

  create_table "supplier_orders", force: :cascade do |t|
    t.float "paid"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.float "price"
    t.string "supplier"
    t.string "order_number"
    t.integer "amount"
    t.bigint "product_id"
    t.index ["product_id"], name: "index_supplier_orders_on_product_id"
  end

Here is my order.rb Concern (`models/concerns/order.rb'):

module Order
  extend ActiveSupport::Concern

  def full_title
    self.product.full_title
  end
end

Right now I'm getting supplier-Value from a global variable, defined in helpers/application_helper.rb:

module ApplicationHelper
  SIZES = %w(1:1 1:2 1:3 1:4 1:5 1:6 1:7 1:8)
  VERSIONS = %w(regular deluxe exclusive)
  COLORS = %w(black blue white)
  SUPPLIERS = %w(A B C D)
end

I don't override any initializers like in linked topics or do anything extraordinary. All (at least for now) other methods do work in the console, but just this one is not.

答案1

得分: 1

在Gemfile的开发组中添加 gem 'pry-rails',然后运行 bundle install,再在rails控制台中执行该命令。

英文:

Add gem 'pry-rails' in your development group in Gemfile and run bundle install and execute the command again in rails console.

huangapple
  • 本文由 发表于 2023年2月6日 20:51:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定