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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:21:33+00:00 2026-05-25T19:21:33+00:00

I have two models with a one-to-one association. class User < ActiveRecord::Base has_one :setting

  • 0

I have two models with a one-to-one association.

class User < ActiveRecord::Base
  has_one  :setting
end

class Setting < ActiveRecord::Base
  belongs_to :user
end

Each model has plenty of fields and user is used quite extensively by a non rails external server, which is why I have separated the tables.

I am trying to use the build_association but all I get is undefined method `build_setting’ for nil:NilClass. I want to do this because I want a single form with fields from both models to setup a new user.

In my user controllers new method I try this:

def new
  @user = User.new
  @setting = @user.setting.build_setting

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @user }
  end
end

Which throws:

NoMethodError in UsersController#new
  undefined method `build_setting' for nil:NilClass

Why? According to the api docs this is the way to do it.

Doing this seems to work, but its not the right way (or is it?):

def new
  @user = User.new
  @setting = Setting.new
  @user.setting=@setting

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @user }
  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-05-25T19:21:33+00:00Added an answer on May 25, 2026 at 7:21 pm

    In your users model add

    class User < ActiveRecord::Base
      has_one  :setting
      validates_associated :setting
    end
    

    and then use

    @setting = @user.build_setting
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two dependent models with one to many association class Post < ActiveRecord::Base
I have the following two models, User.. class User < ActiveRecord::Base has_and_belongs_to_many :sites end
I have two models: User and Store class Store < ActiveRecord::Base belongs_to :user class
I have two models: First: class CountryList < ActiveRecord::Base has_one :country, :dependent => :nullify
I have a simple has_one/belongs_to relationship between two models. This is a new association
I have two models related one-to-many: a Post and a Comment : class Post(models.Model):
I have two tables, one Company and one Employee: class Company(models.Model): name = models.CharField(max_length=60)
I have two models, TreeNode and User. Each user has_one TreeNode, which is the
Let's say I have two ActiveRecord models: LineItem and Article. class LineItem < ActiveRecord::Base
I have three models : class LevelTwoArea < ActiveRecord::Base has_many :places, dependent: :restrict end

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.