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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:43:21+00:00 2026-05-31T08:43:21+00:00

In my Rails app I have Users. Users are asked for their home city

  • 0

In my Rails app I have Users. Users are asked for their home city and district / neighborhood.

class User < ActiveRecord::Base
  belongs_to :city
  belongs_to :district
end

class City < ActiveRecord::Base
  has_many :users
  has_many :districts
end

class District < ActiveRecord::Base
  has_many :users
  belongs_to :city
end

In forms I build the associations using a virtual attribute on the User model that accepts a string (more info below in case it’s relevant).

In the console this all works great, but in the UI it’s not working. The problem seems to be, I can get city_name through a form no problem, but when I try to assign city and district in the same form it always fails. In other words, mass assignment doesn’t work.

@user.update_attributes(params[:user])

Instead the only thing I have been able to figure out is to manually set each key from a form submission, like:

@user.name = params[:user][:name] if params[:user][:name]
@user.city_name = params[:user][:city_name] if params[:user][:city_name]
@user.district_name = params[:user][:district_name] if params[:user][:district_name]

This approach works, but it’s a pain, kind of brittle, and feels all wrong because it starts gunking the controller up with a lot of logic.

My question is:

  1. Is there a way to create or update attributes in a specific order, ideally in the model so that the controller doesn’t have to worry about all this?

  2. Am I doing this wrong? If so, what approach would be better.


Extra Info

Here’s how I build the associations using virtual attributes on the user model, in case that’s relevant to any potential answerers:

I want users to be able to select a city by just typing in a name, like “Chicago, IL”. This works fine, using a virtual attribute on the user model like so:

def city_name
  city.try :full_name
end

def city_name=(string)
  self.city = City.find_or_create_by_location_string( string )
end

It only makes sense for a user to find or create a district from the city they’ve chosen. This works slightly differently:

def district_name
  district.try :name
end

def district_name=(string)
  if self.city.nil?
    raise "Cannot assign a district without first assigning a city."
  else
    self.district = self.city.districts.find_or_create_by_name( string )
  end
end

In the model layer these things work fine, as long as both a city_name and district_name are set the district association works as expected.

  • 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-31T08:43:23+00:00Added an answer on May 31, 2026 at 8:43 am

    I think you could do a few things to clean this up. First, you can use delegates to clean up the code. e.g.

    delegate :name, :to => :city, :prefix => true, :allow_nil => true
    

    that way you can do something like

    user = User.create
    user.city_name # nil
    city = City.create(:name => 'chicago')
    user.city = city
    user.save
    user.city_name # chicago
    

    and it will just work.

    Next, I would say take the name-to-id logic out of your model. You can do it either in the form (e.g. an ajax search puts the district id/city id into a hidden field), or in the controller. Then just assign the city/district as normal.

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

Sidebar

Related Questions

I have a rails app where I have clusters and users in a belongs_to
I have a Rails app with Users, and each user HABTM Roles. I want
Currently I have a rails app where user profiles are viewed at /users/:id, but
I have built a ruby on rails app that lets users track their workouts.
I have built a ruby on rails app that lets users track their workouts.
I have built a ruby on rails app that lets users track their workouts.
I have a Rails app (running 3.2.2) and I want to cache my users'
I have a rails app with Devise 1.4.9. Currently, it allows only users from
I have a Rails 3 app that lets users enter comments. I want any
I have a Rails 3.1.3 app which uses devise for users authentication and soft-deletes

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.