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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:02:47+00:00 2026-06-09T03:02:47+00:00

Note: This question was spawned from another question I had regarding the use of

  • 0

Note: This question was spawned from another question I had regarding the use of accepts_nested_attributes_for. You may reference that question for additional context, if needed.

I believe this question is best explained with a simple example:

class Foo < ActiveRecord::Base
  has_many :bars, inverse_of: :foo
end

class Bar < ActiveRecord::Base
  validates :foo_id, presence: true

  belongs_to :foo, inverse_of: :bars
end

f = Foo.new()
=> #<Foo id: nil, created_at: nil, updated_at: nil>
b = f.bars.build()
=> #<Bar id: nil, foo_id: nil, created_at: nil, updated_at: nil>
f.save!
=> ActiveRecord::RecordInvalid: Validation failed: Bars foo can't be blank

Is there an easy way to fix this problem? I know that I could save f first and then build b, but my situation is a little more complex than this example (see the question I referenced above) and I’d prefer to avoid that if possible.

  • 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-09T03:02:49+00:00Added an answer on June 9, 2026 at 3:02 am

    The child records get created at the same time as parent, this is why your validation is failing, your child is not yet persisted. to make it work i would write a custom validation like this

    class Foo < ActiveRecord::Base
      has_many :bars
      accepts_nested_attributes_for :bars, :allow_destroy => true
    
    end
    
    class Bar < ActiveRecord::Base
      belongs_to :foo
      validates_presence_of :bar
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(note that this question is not about CAS, it's about the May fail spuriously
Please note that this question is from 2008 and now is of only historic
Note: This question has broadened in scope from previous revisions. I have tried to
NOTE: This question is only loosely related to tinyxml, however including details like that
NOTE: This question is related to CodeIgniter-RestServer When I call model.save() from backbone the
Note: This question and answer is a full copy from Kelly Brownsbergers Blog .
[Note: This question had the original title " C (ish) style union in C#
NOTE : This question was asked from the viewpoint of ECMAScript version 3 or
Note: This question was marked as solved once, but it figured out that upgrading
Note: This is an extension of an earlier question I asked here: Do additional

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.