Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:32:24+00:00 2026-06-17T09:32:24+00:00

I have 2 models Users and Companies. (I’m using Devise for User) User belongs

  • 0

I have 2 models Users and Companies. (I’m using Devise for User)

  • User belongs to Company.
  • Company has many Users.

My User model includes an client_id column.

At the moment a User signs-up and is directed to the new_company_path where I’d like to create the relationship. (I’d prefer to keep this in 2 steps).

I know my code is wrong here in the companies_controller.rb — but it’s where I’m at

  def create
    @user = current_user
    @company = @user.Company.new(params[:company])

    respond_to do |format|
      if @company.save
        format.html { redirect_to root_path, notice: 'Company was successfully created.' }
        format.json { render json: @company, status: :created, location: @company }
      else
        format.html { render action: "new" }
          format.json { render json: @company.errors, status: :unprocessable_entity }
      end
    end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:32:25+00:00Added an answer on June 17, 2026 at 9:32 am

    Your problem lies within the line

    @company = @user.Company.new(params[:company])
    

    The association from user to company should not be accessed with a capital letter. To get the company associated with a user, you should call it like this:

    @user.company
    

    However, if there is no company associated then that method will return nil and you cannot call .new on nil so instead you need to call another method that Rails creates for you called build_company like this:

    @company = @user.build_company(params[:company])
    

    The last problem is that since it is the user that belongs to the company, the User instance needs to be updated with the newly created company_id and that will not happen if you only save the company. But when you use the build_company method, it will store the company instance in the association from User so if you call save on the user instead of the company it will create the company and link it to user, like this:

    def create
      @user = current_user
      @user.build_company(params[:company])
    
      respond_to do |format|
        if @user.save
          format.html { redirect_to root_path, notice: 'Company was successfully created.' }
          format.json { render json: @user.company, status: :created, location: @user.company }
        else
          format.html { render action: "new" }
          format.json { render json: @user.company.errors, status: :unprocessable_entity }
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have 3 models users, companies and roles User belongs_to role User has and
I have a User model, Person model and Company model. a User has many
I have these models: class UserProfile(models.Model): user = models.OneToOneField(User) #etc class Organization(models.Model): users =
I have a users model and a companies model. Many users to many companies.
I'm using Cancan, Devise, Rails 3 for my ordering application. Each user has many
I have two models: Company and User they have a has_and_belongs_to_many relationship. I'm using
I have two models - Tenant and User, each tenant will have_many users and
I have an Address model which belongs to ParkingCompany or User. each address can
I have 3 model class User < ActiveRecord::Base has_many :profiles has_many :companies, :through =>
I have to objects, User and Company and they have a one to many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.