简体   繁体   English

Rails将记录写入表

[英]Rails writing a record to a table

I'm a newbie. 我是新手。 I have a table called statuses. 我有一个称为状态的表。 It contains status changes for records in a table called requests. 它包含称为请求的表中记录的状态更改。

When a user creates a new request, I'd like to write a record to statuses. 用户创建新请求时,我想写一条状态记录。

How would I go about that? 我将如何处理?

Thanks 谢谢

You can use after_save callback: 您可以使用after_save回调:

class Record < ActiveRecord::Base

  after_save :add_status_chanage

  def add_status_change
    status = Status.new
    status.record_id = id
    status.field = field
    status.save
  end

end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM