以下是我的模特协会:
Expense belongs_to Supplier belongs_to Concept
# For example:
"ADSL Connection and Telephone July 2" > Comcast > Internet
"Water Bill August 20" > PG&E > Water
我正在尝试列出我的所有费用,同时也展示了它所支付的公司以及它们所属的概念。
bullet
gem正在提醒我一个N + 1查询,并建议我加入它:
N+1 Query detected
Supplier => [:payment_concept]
Add to your finder: :include => [:payment_concept]
在我的控制器中,我已经包含了供应商(针对另一个N + 1)问题,但是当我尝试包含:payment_concept
时,我收到的错误是它不是公认的符号:
@expenses = Expense.all(:include => [:supplier, :payment_concept])
# In my view:
.rows
- for expense in @expenses
.item
p.date.lato= expense.date_made
p.supplier.lato= expense.supplier.name
p.payment_concept.lato= expense.supplier.payment_concept.name
p.payment_type.lato= expense.payment_type.name
p.account.lato= expense.account
p.price.lato= number_to_currency(expense.price)
.item-actions
= link_to '<i class="icon-remove"></i>'.html_safe, '#', class: 'small-button'
可以理解的是,Expense只与供应商有关系,而不是直接与供应商有关系。
有什么建议吗?
答案 0 :(得分:1)
你尝试过这样的事吗?
@expenses = Expense.all(:include => [:supplier => :payment_concept])
编辑:
为了澄清,由于Concept
属于Supplier
(属于Expenses
),因此您需要在includes
语句中使用哈希关系。