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

  • Home
  • SEARCH
  • 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 3599654
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:23:40+00:00 2026-05-18T20:23:40+00:00

I have these classes: class User has_one :user_profile accepts_nested_attributes_for :user_profile attr_accessible :email, :password, :password_confirmation,

  • 0

I have these classes:

class User
  has_one :user_profile
  accepts_nested_attributes_for :user_profile
  attr_accessible :email, :password, :password_confirmation, :user_profile_attributes
end

class UserProfile
  has_one :contact, :as => :contactable
  belongs_to :user
  accepts_nested_attributes_for :contact
  attr_accessible :first_name,:last_name, :contact_attributes
end

class Contact
   belongs_to :contactable, :polymorphic => true 
   attr_accessible :street, :city, :province, :postal_code, :country, :phone
end

I’m trying to insert a record into all 3 tables like this:

consumer = User.create!(
  [{
  :email => 'consu@a.com',
  :password => 'aaaaaa',
  :password_confirmation => 'aaaaaa',
  :user_profile => {
      :first_name => 'Gina',
      :last_name => 'Davis',
      :contact => {
        :street => '221 Baker St',
        :city => 'London',
        :province => 'HK',
        :postal_code => '76252',
        :country => 'UK',
        :phone => '2346752245'
    }
  }
}])

A record gets inserted into users table, but not into the user_profiles or contacts tables. No error occurs either.

What’s the right way to do such a thing?

SOLVED
(thanks @Austin L. for the link)

params =  { :user =>
    {
    :email => 'consu@a.com',
    :password => 'aaaaaa',
    :password_confirmation => 'aaaaaa',
    :user_profile_attributes => {
        :first_name => 'Gina',
        :last_name => 'Davis',
        :contact_attributes => {
            :street => '221 Baker St',
            :city => 'London',
            :province => 'HK',
            :postal_code => '76252',
            :country => 'UK',
            :phone => '2346752245'
          }
      }
  }
}
User.create!(params[:user])
  • 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-18T20:23:41+00:00Added an answer on May 18, 2026 at 8:23 pm

    Your user model needs to be setup to accept nested attributes via accepts_nested_attributes

    See the Rails documentation for more info and examples: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

    Edit: Also you might want to consider using has_one :contact, :through => :user_profile which would allow you to access the contact like this: @contact = User.first.contact.

    Edit 2: After playing around in rails c the best solution I can find is this:

    @c = Contact.new(#all of the information)
    @up = UserProfile.new(#all of the information, :contact => @c)
    User.create(#all of the info, :user_profile => @up)
    

    Edit 3: See the question for a better solution.

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

Sidebar

Related Questions

I have these classes: class User has_one :user_profile accepts_nested_attributes_for :user_profile attr_accessible :email, :password, :password_confirmation,
I have two classes: class User < ActiveRecord::Base :has_one :foo end class Foo <
I have these classes: class Base { ... private: std::vector<X> v; }; class Derived
Hi i have these classes: class Core < ActiveRecord::Base belongs_to :resource, :polymorphic => true
I have these simple classes Class A(models.Model): ... Class Meta(models.Model): a = models.OnetoOneField(A, primary_key=True)
Concerning data binding I have these classes: public class Foo : List<Bar> { public
In a rails 3 app I'm building I have these three classes: class Instrument
I have these two classes public class Person { } public class Company {
Let's say we have these two classes: public class Base { public static int
I have these classes. class Author{ Person person } class Person{ String lastName String

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.