在我的订单模型中,我有下面的回调。一切正常,直到最后一行,我尝试更新1发票属性并在发票模型中触发after_update回调,但是当我使用.update()方法时,我收到错误的参数数量(0表示1)。 p>
def update_invoice # self = order
...
invoice = Invoice.find_or_create_by(job_id: job.id, start_string: start_string, end_string: end_string, client_id: client.id, start_date: start_date, end_date: end_date)
self.update_column(:invoice_id, invoice.id)
statement_start_date = start_date.beginning_of_month
statement_end_date = end_date.end_of_month
statement = Statement.find_or_create_by(start_date: statement_start_date, end_date: statement_end_date, client_id: client.id)
self.update_column(:statement_id, statement.id)
invoice.update(statement_id: statement.id) # wrong number of arguments (0 for 1) error
end
以下作为解决方法:
invoice.update_column(:statement_id, statement.id)
invoice.save
为什么第一种方法不能像我预期的那样工作?谢谢!
答案 0 :(得分:4)
update是一种类方法。这应该有效:
Invoice.update(invoice.id, :statement_id => statement.id)
你也可以使用update_attribute
invoice.update_attribute('statement_id', statement.id)
答案 1 :(得分:0)
update方法仅适用于班级。
使用
invoice.update_attribute(:statement_id, statement_id) #validations will be skipped
或
invoice.update_attribute(:statement_id => statement_id)
不要使用update_column
,因为使用validations
callbacks
和update_column