【问题标题】:Rails 7.0 `Object.group` delivers Object doesn't support #inspectRails 7.0 `Object.group` 交付对象不支持#inspect
【发布时间】:2023-02-06 22:44:28
【问题描述】:

我有来自不同供应商的订单。我想对上述供应商支付的总金额进行分组。

我的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

现在,控制台中的一个简单的 SupplierOrder.group(:supplier) 为我提供了:

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

如果有帮助,请看我的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

这是我的order.rb Concern(`models/concerns/order.rb'):

module Order
  extend ActiveSupport::Concern

  def full_title
    self.product.full_title
  end
end

现在我从全局变量中获取supplier-Value,在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

我不会重写链接主题中的任何初始值设定项,也不会做任何特别的事情。所有(至少现在)其他方法都可以在控制台中使用,但只有这个不行。

【问题讨论】:

  • 你能分享Order模块的内容吗?
  • 当然,将其添加到我的编辑中
  • 您是否在某处定义了 Supplier 类/模块?
  • 不,SupplierOrder 中的 supplier 只是一个文本值。来自helper/application_helper.rb 中定义的常量
  • 是的,我只是在想这可能会导致字符串字段与潜在的 Supplier 模型发生冲突。我猜 puts SupplierOrder.firstSupplierOrder.first.supplier 工作得很好,对吧?

标签: ruby-on-rails ruby ruby-on-rails-7


【解决方案1】:

在 Gemfile 的开发组中添加 gem 'pry-rails' 并运行 bundle install 并在 Rails 控制台中再次执行命令。

【讨论】:

  • 那是我现在的控制台输出:pry(main)&gt; SupplierOrder.group(:supplier) SupplierOrder Load (1.1ms) SELECT "supplier_orders".* FROM "supplier_orders" GROUP BY "supplier_orders"."supplier" =&gt; #&lt;SupplierOrder::ActiveRecord_Relation:0x4038&gt;
  • 你能明确选择 supplier_orders.supplier 并重试吗 像SupplierOrder.select("supplier_orders.supplier").group(:supplier)
猜你喜欢
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 2015-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-10
相关资源
最近更新 更多