我有两个模型:
class User < ActiveRecord::Base
before_create :add_address
has_one :address
def add_address
self.address_id ||= Address.generate_new.id
end
end
和
class Address < ActiveRecord::Base
belongs_to :user
def self.generate_new
new_address = # some code generating UUID
Address.create!({address: new_address})
end
end
但是当我创建用户时,它会保存地址,但地址模型上没有显示 user_id。创建用户后我不想更新地址。也许我做的不是轨道方式?