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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:20:52+00:00 2026-05-23T09:20:52+00:00

With the following models: class Location < ActiveRecord::Base has_many :group_locations has_many :groups, :through =>

  • 0

With the following models:

class Location < ActiveRecord::Base
  has_many        :group_locations
  has_many        :groups, :through => :group_locations
  accepts_nested_attributes_for :group_locations
end

class GroupLocation < ActiveRecord::Base
  belongs_to  :group
  belongs_to  :location
end

class Group < ActiveRecord::Base
  has_many        :group_locations
  has_many        :locations, :through => :group_locations
end

the following commands in rails console does not update the associated records:

>> l = Location.find(1)
=> #<Location id: 1, phone: "(949) 788-9999", ... created_at: "2011-06-02 00:58:07",
updated_at: "2011-06-07 23:57:32">

\>\> l.group_locations
=> [#<GroupLocation group_id: 4, location_id: 1, created_at: "2011-06-02 00:58:07",
updated_at: "2011-06-02 00:58:07">, #<GroupLocation group_id: **37**, location_id: 1,
created_at: "2011-06-02 00:58:07", updated_at: "2011-06-02 00:58:07">]

>> l.update_attributes(:phone => "(949) 788-9998", :group_locations_attributes =>
[{:group_id => 4, :location_id => 1}, {:group_id => **38**, :location_id => 1}])
=> true

>> l
=> #<Location id: 1, phone: "(949) 788-9998", ... created_at: "2011-06-02 00:58:07",
updated_at: "2011-06-08 02:05:00">

>> l.group_locations
=> [#<GroupLocation group_id: 4, location_id: 1, created_at: "2011-06-02 00:58:07",
updated_at: "2011-06-02 00:58:07">, #<GroupLocation group_id: **37**, location_id: 1,
created_at: "2011-06-02 00:58:07", updated_at: "2011-06-02 00:58:07">]

Note that the update_attributes call attempts to change the second GroupLocation to have group_id = 38, but the change is not made (even though the phone number did change). After looking at the code generated when this was implemented in the controller and view, changing the array to a hash (which is what is created in that case) has no different results (and the form/controller) have the same effect of not updating the associated records even though the main record is updated.

Any idea what I need to do to get the nested attributes to update?

  • 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-23T09:20:52+00:00Added an answer on May 23, 2026 at 9:20 am

    The actual answer is that the nested attributes must be accessible via attr_accessible. “accepts_nested_attributes” will only do what I want if it is accompanied by “attr_accessible :group_locations”.

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

Sidebar

Related Questions

I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have the following models: class FieldEntryValue < ActiveRecord::Base belongs_to :field_entry end and class
I have a the following rails models: class Release < ActiveRecord::Base has_many :release_questionnaires, :dependent
I have a nested form with the following models: class Incident < ActiveRecord::Base has_many
I have the following three models (Rails 2.3.8) class Outbreak < ActiveRecord::Base has_many :incidents,
I have the following two classes: class Menu < ActiveRecord::Base has_many :menu_headers accepts_nested_attributes_for :menu_headers
Newbie question. I have the following models: class Asset < ActiveRecord::Base belongs_to :assetable, :polymorphic
I have the following models set up: class Player < ActiveRecord::Base has_many :game_players has_many
I have the following models: class Merchant acts_as_authentic has_one :store accepts_nested_attributes_for :store end class
Given the following example: class AnonymousSession << Struct.new(:location, :preferences) def valid? ... end def

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.