Ruby on rails Ruby,合并两个不同的对象
我需要在RubyonRails应用程序中合并到不同的对象。我有我的发票对象:Ruby on rails Ruby,合并两个不同的对象,ruby-on-rails,ruby,Ruby On Rails,Ruby,我需要在RubyonRails应用程序中合并到不同的对象。我有我的发票对象: class Invoice < ActiveRecord::Base { :id => :integer, :user_id => :integer, :description => :text, ....
class Invoice < ActiveRecord::Base {
:id => :integer,
:user_id => :integer,
:description => :text,
......
:status => :string,
:price => :float
}
但返回一个数组的数组:
[
[{invoice},{payment}],
[{invoice},{payment}],
...
]
我想回报的是:
[
{invoice, :email_payment, :created_at_payment},
{invoice_1, :email_payment, :created_at_payment},
...
]
如何才能做到这一点?我将添加
电子邮件支付
和在付款时创建
作为发票模型的方法,但您可以通过以下方法实现:
user.invoices.where(hide: false).map do |invoice|
invoice.attributes.merge({
email_payment: invoice.payments.last.email,
created_at_payment: invoice.payments.last.created_at
})
end
你不能在你的
Invoice
类中创建email\u payment
和created\u at\u payment
方法,并且只使用Invoice
对象吗?是的,但是从逻辑的角度来看,这没有多大意义,我希望有更好的解决方案!这太完美了,正是我想要的!
[
[{invoice},{payment}],
[{invoice},{payment}],
...
]
[
{invoice, :email_payment, :created_at_payment},
{invoice_1, :email_payment, :created_at_payment},
...
]
user.invoices.where(hide: false).map do |invoice|
invoice.attributes.merge({
email_payment: invoice.payments.last.email,
created_at_payment: invoice.payments.last.created_at
})
end