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 7868423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:59:35+00:00 2026-06-03T00:59:35+00:00

I have a User class defined like this class User end And I have

  • 0

I have a User class defined like this

class User
end

And I have sublassed this to create an Owner class and created a has_one relationship with another Company class

class Owner < User
  has_one :company
end

class Company
  belongs_to :owner
end

In my Users controller when creating a new User I want to accomplish the following:

  1. Create a new User
  2. Create a new Company
  3. Associate the User with the Company (as the Owner i.e. company.owner_id)

I can accomplish this with the following code (simplified for brevity)

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

  if @user.save
    @company.owner_id = @user.id
    @company.save
    ...

Now, this just feels ugly to me, but I can’t seem to get the whole build_asociation process to work as expected (yes, fields are there in both dev and test).

What should I be doing here?

  • 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-03T00:59:38+00:00Added an answer on June 3, 2026 at 12:59 am

    If you need to create Owner and Company at the same time, I suggest you use accepts_nested_attributes_for in Owner. Here’s the code:

    class Owner < User
      has_one :company
      accepts_nested_attributes_for :company
    end
    

    Then in your controller you can do something like this:

    def create
      @user = User.new(params[:user]) # should it be User or Owner?
      @user.company_attributes = params[:company] # assume two separate forms for User and Company
      # if you use fields_for, however, company attributes are nested under params[:user] automatically.
    
      if @user.save
        # do your job here
      end
    end
    

    For complete reference, please take a look at Active Record Nested Attributes and the view helper fields_for

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a User table defined like this: CREATE TABLE Users( UserId int IDENTITY(1,1)
I have a class that defined a user defined operator for a TCHAR*, like
In my application I have a class which has properties of user-defined types like
I have this user defined function. public partial class UserDefinedFunctions { static int i;
I have a contact info class defined like this: class ContactInfo include Mongoid::Document validates_presence_of
I have a user model: class User < ActiveRecord::Base end This model is used
I have a user defined class, say import java.util.Calendar; public class Employee{ private String
I have three tables which are defined as: class User(Base): __tablename__ = 'users' id
I have a user control with a property defined as follows: public partial class
I have two classes, user and role, defined as: public class User : Entity

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.