Ruby on rails 是否有和属于多个和保存后的问题
我有两个模型Ruby on rails 是否有和属于多个和保存后的问题,ruby-on-rails,Ruby On Rails,我有两个模型 class Payment < ActiveRecord::Base has_and_belongs_to_many :invoices after_save :update_invoices_state def update_invoices_state self.invoices.each{|i| i.update_state } end end class Invoice < ActiveRecord::Base has_and_
class Payment < ActiveRecord::Base
has_and_belongs_to_many :invoices
after_save :update_invoices_state
def update_invoices_state
self.invoices.each{|i| i.update_state }
end
end
class Invoice < ActiveRecord::Base
has_and_belongs_to_many :payments
def pending_value
paid_value = Money.new(0,self.currency)
self.payments.each{|payment| paid_value += payment.value}
self.value - paid_value
end
def update_state
if self.pending_value.cents >= 0
if self.due_on >= Time.zone.today
new_state = :past_due_date
else
new_state = :pending
end
else
new_state = :paid
end
self.update_attribute :state, new_state
end
end
classpayment=0
如果self.due\u on>=Time.zone.today
新状态=:过期日期
其他的
新状态=:挂起
结束
其他的
新州=:已支付
结束
self.update_属性:状态,新_状态
结束
结束
我一直在调试它,发现当invoice.update\u状态为run self.payments时,它是空的。看起来HABTM还没有更新
我怎样才能解决这个问题呢?我相信HABTM已经被HASU many:THOW取代了 您将创建一个连接模型,类似于“InvoicePayment”(或其他创造性的东西)
classpayment:发票付款
结束
类InvoicePayment:发票\u付款
结束
这应该可以解决您的问题。如何创建或更新付款和发票?我使用验证解决了这一问题:发票是此帐户的发票,:添加后=>:更新后发票状态,:删除后=>:更新后发票状态出于某种原因,在这种情况下,关联是更新的我正在使用付款。创建(…,:发票=>[…,…)如果你解决了问题,请考虑回答你的问题:
class Payment < ActiveRecord::Base
has_many :invoice_payments
has_many :invoices, :through => :invoicepayments
end
class InvoicePayment < ActiveRecord::Base
belongs_to :invoice
belongs_to :payment
end
class Invoice < ActiveRecord::Base
has_many :invoice_payments
has_many :payments, :through => :invoice_payments
end