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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:53:58+00:00 2026-05-12T16:53:58+00:00

I have a model with a couple of accepts_nested_attributes_for. There is a requirement that

  • 0

I have a model with a couple of accepts_nested_attributes_for. There is a requirement that I have at least one of each of the nested attributes when saving and moving on.

However, validation occurs pre-save, so when I’m removing an item and moving on, it let’s it through.

How can I validate that when I’ve saved, I have at least one item of each nested type there?

  • 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-12T16:53:58+00:00Added an answer on May 12, 2026 at 4:53 pm

    There’s a bug with accepts_nested_attributes_for. Meaning you have to be a little more devious when it comes to validations in the parent model.

    You could use an :after_save callback in each of your nested models to check if it’s the last one. But if there’s many nested associations where you want to ensure at least one, this isn’t very DRY.

    This is however a valid workaround for the linked bug:

    class Whatever < ActiveRecord::Base
      :has_many => :association_a
      :has_many => :association_b
    
      def ensure_minimum_associations
        bad_associations =  [:association_a, :association_b].
          select{|assoc| self.send(assoc).all?{|a| a.marked_for_destruction?}}
        unless bad_associations.empty?
          bad_associations.each do |association|
            errors.add_to_base "Each #{self.class.name.downcase} must retain at least one #{association}"
          end
          return false
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view model with a couple of properties that are not displaying
I have a model that already has a couple dozen of columns that will
I have built mini widgets that consist of a model, a controller a couple
In my model I have a couple of queries that can be used (and
I have a view model that needs to encapsulate a couple of many-to-many relationships.
I have a model that has one column right now, description , that populates
I have model Article it has field title with some text that may contain
Suppose I have an model object Event that contains a Venue . When I
I have a model like this with a couple of fields such as name,
I have a couple of queries for a web site that take a long

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.