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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:07:09+00:00 2026-06-08T06:07:09+00:00

As I learned update_only doesn’t work for has_many association. I am updating nested attributes

  • 0

As I learned update_only doesn’t work for has_many association.

I am updating nested attributes and it creates new children each time and don’t delete old children. What would be the good way to delete them automatically on each update (to mimic :update_only behavior which works for has_one)?

I know about :allow_destroy. However, I don’t have children id’s to let rails know what should be destroyed.

P.S. I asked this question originally back in 2012 and back then I found a hacky solution to override assign_nested_attributes_for_collection_association. I wonder whether anything was introduced in Rails to solve this problem. It looks like very common straightforward case. I wonder why there is a built-in solution for it.

  • 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-08T06:07:11+00:00Added an answer on June 8, 2026 at 6:07 am

    The best approach which I found is to override method assign_nested_attributes_for_collection_association on the object, check association type in there. if it’s has_many then do destroy_all on this association.

    Such code can be generalized and moved to a module for further reuse.

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

Sidebar

Related Questions

Update: making the userIP static seemed to work. However, I learned that MainPage() is
I learned, that it is (at least in most cases) a good propgramming style
I learned about $_SESSION about several weeks ago when creating a login page. I
I learned the object-oriented in Java. Now in develop in C#. This means that
I learned how to create a view controller and make the view slide in
I learned of gist.github.com from Mozilla Ubiquity , and have been using it to
I learned an example of usage of sockets. In this example a client sends
I learned that when executing commands in Python, I should use subprocess. What I'm
I learned something about DLL's yesterday. But I haven't found the way how to
I learned HTML and CSS about 8 months ago, and recently, about 2 months

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.