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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:48:09+00:00 2026-05-25T15:48:09+00:00

I have a one to one relationship with a simple users model and a

  • 0

I have a one to one relationship with a simple users model and a profiles model:

models/user

class User < ActiveRecord::Base
  authenticates_with_sorcery!

  attr_accessible :email, :password, :password_confirmation

  has_one :profile, :dependent => :destroy

  validates_presence_of :password, :on => :create
  validates :password, :confirmation => true,
                       :length       => { :within => 6..100 }

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, :presence        => true,
                    :format          => { :with => email_regex },
                    :uniqueness      => {:case_sensitive => false},
                    :length          => { :within => 3..50 }
end

models/profile

            # == Schema Information
    #
    # Table name: profiles
    #
    #  id         :integer         not null, primary key
    #  weight     :decimal(, )
    #  created_at :datetime
    #  updated_at :datetime
    #

    class Profile < ActiveRecord::Base
      attr_accessible :weight

      belongs_to :user

    end

I am doing it this way because I would like users to be able to track weight over time as well as store other more static data like height in profiles.

However, my new and create methods don’t seem to be working correctly. I on submit of the new action I get this error:

undefined method `build' for nil:NilClass

profile_controller

class ProfilesController < ApplicationController

  def new
    @profile = Profile.new if current_user
  end

  def create
    @profile = current_user.profile.build(params[:profile])
    if @profile.save
      flash[:success] = "Profile Saved"
      redirect_to root_path
    else
      render 'pages/home'
    end
  end

  def destory
  end

end

and profile view for new

<%= form_for @profile do |f| %>
    <div class="field">
        <%= f.text_field :weight %>
    </div>
    <div class="actions">
        <%= f.submit "Submit" %>
    </div>
<% end %>

Thanks in advance for any help you might be able to give. Noob 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-05-25T15:48:09+00:00Added an answer on May 25, 2026 at 3:48 pm

    The build syntax for has_one association is different from has_many association.
    Change your code as follows:

    @profile = current_user.build_profile(params[:profile])
    

    Reference: SO Answer

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

Sidebar

Related Questions

I have a simple user model that stores an email address, crypted_password, salt and
I have a simple one-to-many relationship. I would like to select rows from the
I have a simple has_one/belongs_to relationship between two models. This is a new association
Say you have a one to one relationship in your entity model. The code
I have a one way @OneToMany relationship between a Team and Player class. I
I have a many-to-one relationship that users can edit via checkboxes. PK of Foo
I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I have a simple model which is just a chain of one-to-many relationships: Country
Alright so I have a simple has_many :through relationship in Rails 3.1 class Event
I have a one-to-one relationship but hibernatetool complains when generating the schema. Here's an

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.