将所有交易总和 current_user
Sum all transaction by current_user
我有一个交易模型
create_table "transactions", force: :cascade do |t|
t.bigint "hire_id"
t.bigint "withdraw_id"
t.bigint "user_by_id"
t.bigint "user_to_id"
t.string "reference"
t.integer "status"
t.integer "price"
t.integer "transaction_type"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["hire_id"], name: "index_transactions_on_hire_id"
t.index ["user_by_id"], name: "index_transactions_on_user_by_id"
t.index ["user_to_id"], name: "index_transactions_on_user_to_id"
t.index ["withdraw_id"], name: "index_transactions_on_withdraw_id"
end
Transaction_type 是一个枚举,所以我在我的事务控制器上有这个
@debit = Transaction.where(transaction_type: "Debit").sum(:price)
@credit = Transaction.where(transaction_type: "Credit").sum(:price)
我想生成 current_user 的总贷方和借方 (current_user.id==user_by_id)
请问我该怎么做?
我试过了
@total_credit_by_user = @credit.group_by(&:user_by_id).sum(:price)
@total_debit_by_user = @debit.group_by(&:user_by_id).sum(:price)
在我看来,我有
<%= @total_credit_by_user[current_user.id] %>
<%= @total_debit_by_user[current_user.id] %>
但我得到 undefined method
+' for :price:Symbol`
当 current_user.id==user_by_id
时,如何通过 current_user 求和总贷方和借方
虽然它们看起来很相似,但 sum method for a Hash is different than the sum method for an ActiveRecord relation. Calling group_by
on a relation returns a Hash object, whose sum
method does not accept a column name as an argument. You can use group 相反,它将执行 GROUP BY
并允许您在 ActiveRecord 关系上使用 sum
。
在这种情况下,如果您需要每个用户的总信用额度图,您可以更新控制器逻辑以将 group
与 sum
结合使用,如下所示:
credits = Transaction.where(transaction_type: "Credit")
@total_credit_by_user = credits.group(:user_by_id).sum(:price)
然后您可以通过传递他们的 ID 来访问所需用户的总积分:
@total_credit_by_user[current_user.id]
如果您只想获取当前用户的总信用额度,您可以将上述查询修改为以下内容:
@total_credit = credits.where(user_by_id: current_user.id).sum(:price)
然后就可以直接在视图中使用@total_credit
值了。
我有一个交易模型
create_table "transactions", force: :cascade do |t|
t.bigint "hire_id"
t.bigint "withdraw_id"
t.bigint "user_by_id"
t.bigint "user_to_id"
t.string "reference"
t.integer "status"
t.integer "price"
t.integer "transaction_type"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["hire_id"], name: "index_transactions_on_hire_id"
t.index ["user_by_id"], name: "index_transactions_on_user_by_id"
t.index ["user_to_id"], name: "index_transactions_on_user_to_id"
t.index ["withdraw_id"], name: "index_transactions_on_withdraw_id"
end
Transaction_type 是一个枚举,所以我在我的事务控制器上有这个
@debit = Transaction.where(transaction_type: "Debit").sum(:price)
@credit = Transaction.where(transaction_type: "Credit").sum(:price)
我想生成 current_user 的总贷方和借方 (current_user.id==user_by_id)
请问我该怎么做?
我试过了
@total_credit_by_user = @credit.group_by(&:user_by_id).sum(:price)
@total_debit_by_user = @debit.group_by(&:user_by_id).sum(:price)
在我看来,我有
<%= @total_credit_by_user[current_user.id] %>
<%= @total_debit_by_user[current_user.id] %>
但我得到 undefined method
+' for :price:Symbol`
当 current_user.id==user_by_id
时,如何通过 current_user 求和总贷方和借方虽然它们看起来很相似,但 sum method for a Hash is different than the sum method for an ActiveRecord relation. Calling group_by
on a relation returns a Hash object, whose sum
method does not accept a column name as an argument. You can use group 相反,它将执行 GROUP BY
并允许您在 ActiveRecord 关系上使用 sum
。
在这种情况下,如果您需要每个用户的总信用额度图,您可以更新控制器逻辑以将 group
与 sum
结合使用,如下所示:
credits = Transaction.where(transaction_type: "Credit")
@total_credit_by_user = credits.group(:user_by_id).sum(:price)
然后您可以通过传递他们的 ID 来访问所需用户的总积分:
@total_credit_by_user[current_user.id]
如果您只想获取当前用户的总信用额度,您可以将上述查询修改为以下内容:
@total_credit = credits.where(user_by_id: current_user.id).sum(:price)
然后就可以直接在视图中使用@total_credit
值了。