【发布时间】:2018-05-31 14:23:16
【问题描述】:
快速提问 - 我有两个模型类 - Transactions 和 Accounts。
帐户模型如下所示:
create_table "accounts", force: :cascade do |t|
t.string "account_name"
t.integer "account_number"
t.boolean "current_asset"
t.boolean "non_current_asset"
t.boolean "current_liability"
t.boolean "non_current_liability"
t.boolean "equity"
t.boolean "cost_of_sales"
t.boolean "operating_expense"
t.boolean "sales"
t.boolean "other_income"
t.boolean "bank"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
交易模型如下所示:
create_table "transactions", force: :cascade do |t|
t.date "date"
t.string "description"
t.string "reference"
t.integer "amount"
t.integer "account_id"
t.boolean "payment"
t.boolean "receipt"
t.integer "bank_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.boolean "vat"
t.integer "vat_amount"
t.integer "transaction_form_id"
t.integer "contact_id"
end
用户添加了一个新事务,该事务通过 collection_select
他们选择一个显示所有帐户的帐户,除了 bank == true (其中bank == true 它代表一个银行帐户)。
当用户选择银行时,他们只能选择银行账户 - bank==true(再次通过集合选择)。
我需要运行一个方法,允许我通过基于 bank_id 而不是 account_id 的 Account 模型调用所有交易金额。那么我如何通过 account_id 或 bank_id 将我的 Transaction 模型的两个不同列与 Accounts 类中的同一列相关联。
该方法看起来像:
<% Account.each do |account| %>
<% if account.bank == true && account.transaction(:bank_id) == account.id %>
<%= account.number %>
<%= account.number %>
<%= account.transactions.sum(:amount) %>
<% end %>
Ps:我知道这是我的观点,而不是控制器,但这是另一个讨论!
【问题讨论】:
-
你能贴出两个模型的代码来描述它们之间的关系吗? (
belongs_to/has_many)
标签: ruby-on-rails