title: How to move a Rails one to many association to a one to one association?
body: <p>User model has_many Companies and Company belongs_to a User.</p>
  
  <p>I now want to change this to a user has_one Company and Company has_one/belongs_to association. I have changed the models to look like this:</p>
  
  <p>user.rb</p>
  
  <pre><code>class User &lt; ApplicationRecord
      has_one :company #was has_many
      accepts_nested_attributes_for :company
      devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  end
  </code></pre>
  
  <p>company.rb</p>
  
  <pre><code>class Company &lt; ApplicationRecord
      belongs_to  :user
      has_many        :employees, inverse_of: :company
      has_many        :quotes, inverse_of: :company
      accepts_nested_attributes_for :employees, reject_if: :all_blank, allow_destroy: true #, :reject_if =&gt; lambda { |e| e.first_name.blank? }
      accepts_nested_attributes_for :quotes, allow_destroy: true
  
      validates_presence_of :user, :co_name, :co_number, :postcode
      validates :co_name, length: { minimum: 2, message: "minimum of 2 chars" }
      validates :co_number, format: { with: /\A([1-9]\d{6,7}|\d{6,7}|(SC|NI|AC|FC|GE|GN|GS|IC|IP|LP|NA|NF|NL|NO|NP|NR|NZ|OC|R|RC|SA|SF|SI|SL|SO|SP|SR|SZ|ZC|)\d{6,8})\z/,
                  message: "must be valid company number" }
      validates :postcode, format: { with: /\A(?:gir(?: *0aa)?|[a-pr-uwyz](?:[a-hk-y]?[0-9]+|[0-9][a-hjkstuw]|[a-hk-y][0-9][abehmnprv-y])(?: *[0-9][abd-hjlnp-uw-z]{2})?)\z/,
                  message: "must be valid postcode" }
  
      enum industry:          [ :financial_services, :architect, :business_consultancy ]
  end
  </code></pre>
  
  <p>and have <code>rake db:reset</code> and have changed this line in my Companies#create method:</p>
  
  <pre><code>@company = current_user.companies.new(company_params)
  </code></pre>
  
  <p>to</p>
  
  <pre><code>@company = current_user.company.new(company_params)
  </code></pre>
  
  <p>But I'm getting a</p>
  
  <pre><code>undefined method `new' for nil:NilClass
  </code></pre>
  
  <p>and I can't see where I'm going wrong. <code>current_user</code> should be available? the has_one association is defined so I should be able to call company.new on it right? I don;t need a foreign_key added to User, or do I?</p>
  
  <p>Can anyone help me with where I'm going wrong? Thank you.</p>