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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:15:57+00:00 2026-06-06T22:15:57+00:00

I have a simple User model which is associated to many Town objects using

  • 0

I have a simple User model which is associated to many Town objects using a join table (has_and_belongs_to_many). Now I’d like to update the towns belonging to a particular user by assigning a list of comma-separated town ids (coming directly from the form sent as a HTTP POST parameter).

The user object is saved using the following controller code:

@current_object.update_attributes(params[:user])

The params[:user] includes town_ids which is, for example, set to 1,4,6.

Unfortunately, this does not update the user-town associations at all. However, if I do it manually, it works beautifully well:

User.find(:first).town_ids = "1,4,6" # this saves automatically

Could it just be that it is not possible to mass-assign these collection_singular_ids fields?

My user model contains the following:

has_and_belongs_to_many :towns

# necessary for mass-assignment, otherwise it results in an exception:
attr_accessible :town_ids

Any help is greatly appreciated.

  • 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-06-06T22:15:58+00:00Added an answer on June 6, 2026 at 10:15 pm

    You have to pass the town_ids as an array:

    User.find(:first).update_attributes(:town_ids=>[1,4,6])
    

    If you pass the ids as a string Rails will attempt to convert the string to an integer:

    "1,4,6".to_i # => 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model like this: class User < ActiveRecord::Base serialize :preferences end
I have a User model in my app, which I would like to store
Simple rails app: I have 2 models, user and intro [which is simply a
FOUND THE ANSWER MYSELF, see below... I'd like to have a very simple user
I have a simple web application which lets the user upload local user data
in a simple drawing application I have a model which has a NSMutableArray curvedPaths
I have a model which is accessible through the Django admin area, something like
I have a Competition model which has a corresponding CompetitionEntry model. I'd like to
I have a user model in which I have a method for seeing if
Let's assume a simple and common scenario. I have a User model with 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.