Rails - Best-Practice: How to create dependent has_one relations

后端 未结 7 664
星月不相逢
星月不相逢 2020-12-04 05:39

Could you tell me whats the best practice to create has_one relations?

f.e. if i have a user model, and it must have a profile...

How could i accomplish that

7条回答
  •  囚心锁ツ
    2020-12-04 06:30

    Your solution is definitely a decent way to do it (at least until you outgrow it), but you can simplify it:

    # user.rb
    class User < ActiveRecord::Base
      has_one      :profile
      after_create :create_profile
    end
    

提交回复
热议问题