Question

I am creating contacts & users in the DB in seed.rb. Using the code below, the contact is saved but the child user is not. What am I missing? I've tried with and without the if statement, and the user is never saved.

Models

class Contact < BaseModel
  #...
  has_one :user
end

class User < BaseModel
  #...
  belongs_to :contact
end

Seed

contact = Contact.where({
  :first_name => "Some",
  :last_name  => "Person",
  :email      => "some.person@domain.com",
  :zip        => "12345"}).first_or_initialize
contact.build_user if contact.user == nil
contact.save!
Was it helpful?

Solution

If your contact was not exist then it won't put contact_id in user model.

You should first save contact or used existing so rails will automatically put contact_id in user model

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top