保存后自动创建记录

时间:2015-02-21 08:39:14

标签: ruby ruby-on-rails-3 ruby-on-rails-4

如何在保存后复制tbl1tbl2的数据?

结构

Tbl1. Customer
id | first_name | last_name | address

Tbl2. Address
id | customer_id | address

1 个答案:

答案 0 :(得分:0)

如果您使用has_many

class Customer
  has_many :addresses

  after_save :create_address


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