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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:06:52+00:00 2026-05-12T18:06:52+00:00

I have a User entity that has a Current Location field (city and country).

  • 0

I have a User entity that has a Current Location field (city and country). To hold this info I created an entity called Location which has_many Users.

I’m not entirely sure if I should put in the User model “has_one” or “belongs_to”, but for what I read if I wanted it to have the foreign key of the location I should put “belongs_to”. I also want to be able to edit the user’s Current Location when editing the User. so I am using nested attributes. But when I edit the user I end up adding a new Location each time without ever associating it to the user that was edited. Can you help me out?

My code is the following:

#User Model
class User < ActiveRecord::Base
  ## Relationships
  belongs_to :current_location, :class_name => 'Location'
  accepts_nested_attributes_for :current_location
end

#Location Model
class Location < ActiveRecord::Base
  #Relationship
  has_many :users
end

# part of the _form_edit.haml
- form_edit.fields_for :current_location do |location_form|
  = location_form.label :location, "Current Location"
  = location_form.text_field :location

#Application Helper
#nested attributes for user and location
def setup_user(user)
  returning(user) do |u|
    u.build_current_location if u.current_location.nil?
  end
end

#in the user controller (added after edit)
def update
    @user = @current_user
    if @user.update_attributes(params[:user])
      flash[:notice] = "Account updated!"
      redirect_to account_url
    else
      render :action => :edit
    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-12T18:06:52+00:00Added an answer on May 12, 2026 at 6:06 pm

    The exact problem you’re facing, as others have pointed out is that your controller is not receiving the location id as it should. Looks to me the location id is being passed through the wrong parameter. Unfortunately a location id doesn’t exist on a new record, so this is not possible in the form.

    Your problem stems from the use accepts_nested_attributes_for on a belongs_to relationship. The behaviour isn’t clearly defined. This appears to be a documented bug. So the accepts_nested_attributes_for should be on a has one or has many side of a relationship.

    Here are some possible solutions:

    1. Move The accepted_nested_attributes_for to the Location model and build your forms the other way around.

      -form_for @location do |location_form|
       ...
       =location_form.fields_for @user do |user_form|
         ....
      

      Unfortunately this doesn’t allow for a logical way of presenting information. And makes editing the right user difficult.

    2. Use a join model, and make a has one :through relationship.

      I’m honestly not sure how well accept_nested_attributes_for works with a :through relationship, but it will definitely solve your problem with linking records.

    3. Ignore accepts_nested_attributes_for and handle the association in your controller the old fashioned way.

      Actually keep the accepts_nested_attributes_for. It provides some handy convenience methods, just don’t let it get to the update_attributes/create statement.

      def update 
        @user = @current_user 
        completed = false
        location_params = params[:user].delete(:current_location_attributes)
      
        User.transaction do
          @location = Location.find_or_create_by_id(location_params)
          @user.update_attributes(params[:user]) 
          @user.current_location = @location
          @user.save!
          completed = true
        end
        if completed
          flash[:notice] = "Account updated!" redirect_to account_url 
        else 
          render :action => :edit 
        end
      end
      

    Fields for will populate an id field in the current_location_attributes hash automatically, if it’s not creating a new location. However, find_or_create_by_id, requires an :id entry in the hash for it to work. It will create with a correctly auto incremented id if the id isn’t in the database. If you are creating a new location you will need to add it. Easiest to add it to the form with =location_form.hidden_field :id, 0 unless current\_location.new\_record?.

    However, you might want to cut down on duplicate location creation, and change the Location.find_or_create_by_id line to Location.find_or_create_by_location. This will also cut down on any errors from failed uniqueness validations.

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

Sidebar

Related Questions

I have a non-ndb entity that is created and a webapp2 User who owns
In our application, I have seen code written like this: User.java (User entity) public
I have a custom entity in Microsoft CRM (4.0). The user has to input
For a current MVC3 project I have a model that has multiple pages for
I have a User class that has @Embedded a class Profile. How can I
I have an entity which has a property that does not get its value
i have this problem that has been buggin me for the last hours. Lets
After reading this tidbit , it would seem that the current user's permission would
If you have a user entity, you could store his/her address in two ways.
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.