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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:28:11+00:00 2026-05-22T15:28:11+00:00

Given the following definitions Foo has_many :bars, :dependent => :nullify, :autosave => false Bar

  • 0

Given the following definitions

Foo
    has_many :bars, :dependent => :nullify, :autosave => false

Bar
    validates_presense_of :foo
    foo_id is not null # database constraint

I would like to re-associate a Bar object to a different Foo object. I need it all correctly done in memory so I can render everything in case after all work validation fails.

The problem is when I do something like

bar.foo.bars.delete(bar)
bar.foo = other_foo
other_foo.bars << bar

I get a failure that at bar.foo.bars.delete(bar) a sql update executes nulling out my bar’s foo_id property. Therefore sql constraint exception and failure. I instead need it to just work all in memory and fail validation if necessary.

Edit:

The thing is this is all in a transaction so I don’t care about db updates, I just don’t want them triggering database constraint issues, yet I want my constraints to be present and not allow a null value in the db for my Bar object. When my save executes, if it succeeds, I want my save to re-associate everything as appropriate. If it fails I want my memory model to be in the correctly updated (but not valid) state, this way I can render my page with the updated values and messages that the update failed.

Honestly my main goal is to have the following situation:

before:

foo1.bars => [bar1, bar2]
foo2.bars => [bar3]

after:

foo1.bars => [bar1]
foo2.bars => [bar3, bar2]

So my main goal is to solve this problem (in-memory full update of all appropriate objects), if it involves not dealing with re-associating like I already have, I’ll accept that too.

  • 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-22T15:28:11+00:00Added an answer on May 22, 2026 at 3:28 pm

    To remove an associated child (bar1) element from the parent’s (foo1) collection of bars you can use

    foo1.bars.delete_if{|b| b.id == bar1.id}
    

    This will remove bar1 from the foo1’s bars collection but bar1 will remain unchanged in the database because delete_if works on the Enumerable on which the HasManyAssociation is built on.

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

Sidebar

Related Questions

No related questions found

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.