【发布时间】: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.first和SupplierOrder.first.supplier工作得很好,对吧?
标签: ruby-on-rails ruby ruby-on-rails-7