简体   繁体   中英

Automatic creation record after save

How do I replicate data of tbl1 in tbl2 after save?

Structure

Tbl1. Customer
id | first_name | last_name | address

Tbl2. Address
id | customer_id | address

If you use has_many

class Customer
  has_many :addresses

  after_save :create_address


  private
  def create_address
    self.addresses.create(address: self.address)
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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