1
我需要在我的Ruby on Rails应用程序中合并到不同的对象。我有我的发票对象:Ruby,合并两个不同的对象
class Invoice < ActiveRecord::Base {
:id => :integer,
:user_id => :integer,
:description => :text,
......
:status => :string,
:price => :float
}
我的支付对象:
class Payment < ActiveRecord::Base {
:id => :integer,
:token => :string,
:invoice_id => :integer,
:created_at => :datetime,
:updated_at => :datetime,
:email => :string
}
1处它们之间有很多关系:
class Invoice < ActiveRecord::Base
has_many :payments
class Payment < ActiveRecord::Base
belongs_to :invoice
现在我想要做的是返回关联的付款对象的发票对象和:电子邮件和:created_at字段。现在我回到这两个对象与ZIP功能:
:invoices => (user.invoices.where(:hide => false).zip user.invoices.map{|x| x.payments.last}),
但返回数组的数组:
[
[{invoice},{payment}],
[{invoice},{payment}],
...
]
我想回是一样的东西:
[
{invoice, :email_payment, :created_at_payment},
{invoice_1, :email_payment, :created_at_payment},
...
]
我怎样才能做到这一点?
无法创建'email_payment'和'在'Invoice' created_at_payment'方法类并只使用'invoice'对象? – Magnuss
是的,但从逻辑的角度来看,这没什么意义,我希望能有更好的解决方案! – ste