ruby on rails - ActiveRecord linked entity not being saved? -


i have user has company, follows

class user < activerecord::base   has_one  :company end  class company < activerecord::base   belongs_to :user end 

in seeds.rb, i'm setting them this

cc = company.find_by_name("socialsky, inc.") || company.create(   name: "whatever inc.",   main_email: "person@example.com" )  client = user.find_by_email('johndoe@gmail.com') || user.create(    email: 'johndoe@gmail.com',   company: cc ) 

later on, on service, i'm doing

user_company = user.find_by_email('johndoe@gmail.com').company 

and it's returning nil

why that? doing wrong?

thanks!

if using rails 3, have add company_id attr_accessible.

class user < activerecord::base    has_one  :company    attr_accessible : email end 

then in seeds file, should first verify if company created or not.

 cc = company.find_by_name("socialsky, inc.") || company.create(    name: "whatever inc.",    main_email: "person@example.com")   p cc 

and later

 client = user.find_by_email('johndoe@gmail.com')  if client.blank?    u = user.new(email: 'johndoe@gmail.com')    u.company_id = cc.id    u.save  end 

Comments