与 ROR Active Record 中的急切加载结果相关
我有一个属于客户的发票模型(当然客户有很多发票)。
如果我这样做:
{
@invs = Invoice.find(
:all,
:include => ["customer"],
:conditions => ['somecondition']
)
}
@invs 的类是一个数组,如果我这样做:@invs[5]
,我可以在数组中看到第 6 张发票的所有属性。但此时我没有看到关联的客户记录属性。为此,我需要执行 @invs[5].customer
。
我的问题是允许从发票记录访问客户记录的 Ruby 结构是什么?我想创建 SomeClass 的新任意记录并将其附加到记录集中的发票记录,以便我可以执行 @inv[5].someclass
操作。这可能吗?如果是这样怎么办?
[编辑-全文] 下面试图解释我为什么要问这个问题。
尽管我的发票模型与数据库中的单个表相关,但我的客户记录可以存档并移动到另一个名为 Archcustomer 的表,该表在结构上与客户相同。因此,我有其他关联,例如发票属于_Archcustomer 和Archcustomer has_many Invoices。
我需要获取发票列表及其关联的客户记录(无论它保存在哪里,即客户或大客户),但按 Customer.category_id 和 Customer.id 和 Invoice.id 排序。
在我上面 @invs 的原始示例中,我包含了客户记录,并且可以包含 :order =>; “customer.category_id、customer.id、invoice.id”子句,但在客户存档的情况下,客户记录为零。
所以我想我会执行以下操作:
{
@invs.each do |inv|
next if inv.customer != nil
archcust = Archcustomer.find(inv.customer_id) #since it's not in customer get it from Archcustomer
inv.customer.category_id = archcust.category_id
etc...
...
end
}
然后使用@inv.sort_by。然而,这会抱怨,因为我试图为 Nil 类的 customer.category_id 分配一个值。
那时我想,如果我可以将任意记录附加到发票上,我可以填写来自 Archcustomer 或 Customer 的信息,并拥有一组通用的属性来调用我的 sort_by 。
当然,如果有其他方法可以实现我想要的一组发票,那么我会很乐意这样做。
I have an Invoice model which belongs to Customer (and of course Customer has_many invoices).
If I do:
{
@invs = Invoice.find(
:all,
:include => ["customer"],
:conditions => ['somecondition']
)
}
The class of @invs is an Array and I can see all the attributes for the 6th invoice in the array if I do: @invs[5]
. However at that point I don't see the associated Customer record attributes. To get that I need to do @invs[5].customer
.
My question is what is the Ruby structure that allows access to the customer record from the invoice record? I want to create a new arbitrary record of SomeClass and attach that to the invoice record within the recordset so that I can do @inv[5].someclass
. Is that possible? and if so how?
[EDIT - FULL STORY]
The following tries to explain why I'm asking the question.
Although my Invoice model relates to a single table in the Db my Customer record can get archived and moved to another table called Archcustomer which is identical to Customer in structure. So I have additional associations like Invoice belongs_to Archcustomer and Archcustomer has_many Invoices.
I need to get a list of Invoices together with their associated customer record (regardless of where it's held i.e. Customer or Archcustomer) but sorted by Customer.category_id and Customer.id and Invoice.id.
In my original example above of @invs I am including the customer record and I could include the :order => 'customer.category_id, customer.id, invoice.id' clause but where the customer is archived the customer record is nil.
So I thought I would do the following:
{
@invs.each do |inv|
next if inv.customer != nil
archcust = Archcustomer.find(inv.customer_id) #since it's not in customer get it from Archcustomer
inv.customer.category_id = archcust.category_id
etc...
...
end
}
and then use @inv.sort_by. However that complains because I'm trying to allocate a value to customer.category_id of Nil class.
That's when I thought if I could attach an arbitrary record to Invoice I could fill in the info from either Archcustomer or Customer and have a common set of attributes to call my sort_by on.
Of course if there is some other way of achieving my set of Invoices ordered as I want them then I'll be happy to go with that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的发票模型中(假设您已建立 Archcustomer 关系):
使用
@invoice.customer_record
而不是@invoice.customer
或@invoice.archcustomer
code> 当您需要从任何可用来源获取记录时。.include
两者都在加载时进行。当您已经拥有真正的
Archcustomer
时,就不必费心将东西分配给虚构的Customer
。只需在customer_record.category_id
上运行sort_by
即可。In your invoice model (assuming you have your Archcustomer relationship in place):
Use
@invoice.customer_record
instead of@invoice.customer
or@invoice.archcustomer
when you need to get a record from any available source..include
both when you're loading.Don't bother assigning stuff to an imaginary
Customer
when you already have a realArchcustomer
. Just runsort_by
oncustomer_record.category_id
.