ruby on rails - generate connected models with the primary at the same time -
i have model user. have model user_info. 1 user has 1 record in user_info , every user_info record belongs 1 user. want when create new user empty record in user_info created.
user.rb
has_one :user_info, :dependent => :destroy
user_info.rb
belongs_to :user validates :user_id, presence: true, uniqueness: true
user_controller.rb
@user = user.new(user_params) @user.user_info.new <----- error "undefined method `new' nil:nilclass" if @user.save.........
the table :users has column :user_info_id , table :user_infos has column :user_id
what do wrong?
i have found answer here: rails 3 undefined method `create' nil:nilclass error while trying create related object
when use has_one, don't use .create has_many. because relation object directly returned, , not "proxy" method has_many. equivalent method create_profile (or create_x x object)
Comments
Post a Comment