English 中文(简体)
与ROR活动记录中的热切加载结果相关
原标题:Related to result of eager loading in ROR Active Record

我有一个属于客户的发票模型(当然客户有很多发票)。

如果我这样做:

{
  @invs = Invoice.find(
    :all, 
    :include => ["customer"], 
    :conditions => [ somecondition ]
  )
}

@invs的类是一个数组,如果我这样做,我可以看到数组中第六张发票的所有属性:@invs[5]。但是,在这一点上,我看不到相关联的Customer记录属性。要做到这一点,我需要执行@invs[5]。customer

我的问题是,允许从发票记录访问客户记录的Ruby结构是什么?我想创建一个新的SomeClass的任意记录,并将其附加到记录集中的发票记录,这样我就可以执行@inv[5]。SomeClass。这可能吗?如果是,怎么办?

[EDIT - FULL STORY] The following tries to explain why I m asking the question.

尽管我的Invoice模型与Db中的一个表相关,但我的Customer记录可以被存档并移动到另一个名为Archcustomer的表中,该表在结构上与Customer相同。因此,我有其他关联,如Invoice belongs_to Archcustomer和Archcustomer has_many Invoices。

我需要获得一个发票列表及其相关的客户记录(无论其保存在哪里,即customer或Archcustomer),但按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分配一个值。

当时我想,如果我可以将任意记录附加到Invoice,我可以填写来自Archcustomer或Customer的信息,并有一组通用的属性来调用我的sort_by。

当然,如果有其他方法可以实现我所订购的发票集,那么我很乐意接受。

最佳回答

在您的发票模型中(假设您已经建立了大客户关系):

class Invoice < ActiveRecord::Base
  def customer_record
    customer or archcustomer
  end
end

当您需要从任何可用来源获取记录时,请使用@invoix.customer_record,而不是@invoix.archcustomer.include当您重新加载时。

当你已经有了一个真实的Archcustomer时,不要麻烦把东西分配给一个虚构的客户。只需在customer_record.category_id上运行sort_by

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签