0
我有一个客户和发票模型。客户有许多发票和发票属于客户。来自关联的访问属性
class Customer < ActiveRecord::Base
attr_accessible :billing_address, :customer_currency, :email, :first_name, :last_name, :mobile, :name, :payment_terms, :phase_type, :pays_vat
validates_presence_of :first_name, :last_name, :mobile, :billing_address, :payment_terms, :phase_type, :customer_currency
has_many :invoices
validates :email,
:presence => true,
:uniqueness => true,
:email_format => true
validates :name, :mobile, :presence => true, :uniqueness => true
end
发票型号是
class Invoice < ActiveRecord::Base
belongs_to :customer
attr_accessible :approved_by, :due_date, :invoice_date, :terms, :customer_id, :customer
validates :invoice_date, presence: true
validates :due_date, presence: true
validates :customer, presence: true
我试图创建一个索引页,其中列出了系统中所有的发票,这将发票显示谁发票所属的客户名称。我如何检索并在我的模型和视图中清楚地描述它?
你应该使用发票。包括(:customer)。所有这些都是为了在迭代发票时避免额外的查询。 –
不,如果关联是正确的,它将按照我所示的方式工作。 – alex
肯定会的,但如果有10张发票,则必须进行11次sql查询 - 获取所有发票和每个发票的1(获取客户的first_name)。如果使用'includes'方法,则只有2个查询。 –