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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:22:16+00:00 2026-05-25T03:22:16+00:00

Is it possible to perform validations when creating new instances of an associated model

  • 0

Is it possible to perform validations when creating new instances of an associated model within a before_save callback in ruby?

class Podcast < ActiveRecord::Base
has_many :tracks, :dependent=>:destroy

before_save :generate_tracks

# creates the tracks played in the podcast 
def generate_tracks
    json = Hashie::Mash.new HTTParty.get("#{self.json_url}")
    json.sections.each do |section|
      if section.section_type=="track"
        track = self.tracks.build :name=>section.track.name
      end
    end
  end
end 

The above code works fine but I was hoping to add something like this inside the if statement:

unless track.valid?
  errors[:base] << "OOPS, something went wrong whilst trying to build tracklist."
  return false
end

The problem with this code is that track.valid? always returns false because the Track model validates the presence of podcast_id. I don’t feel so comfortable doing this in an after_create callback because I want to actually cancel podcast creation if the tracklist doesn’t validate too. So what can I do?

  • 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-25T03:22:17+00:00Added an answer on May 25, 2026 at 3:22 am

    Sounds to me as though what you want is validates_associated, which would let you do:

    class Podcast < ActiveRecord::Base
      has_many :tracks
    
      validates_associated :tracks
    end
    

    That way, a podcast won’t save unless it’s associated tracks are valid.

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

Sidebar

Related Questions

Is it even possible to perform address (physical, not e-mail) validation? It seems like
Is it possible to perform a named-group match in Perl's regex syntax as with
Is it possible to perform a global reversed-find on NHibernate-managed objects? Specifically, I have
Is it possible to perform some custom processing when Windsor instantiates a type? Something
Is it possible to perform an action when a UITableViewCell is highlighted? For example
In SQL Server is it possible to perform table partition without having to recode
What are possible reasons why NHibernate does not perform delete operation? public bool Delete(MyType
can i perform select single node from IXMLDOMElement ,if its possible give me one
Is it possible to use a jQuery ajax call to perform Forms Authentication with
Is it possible for the nose unit testing framework to perform tests during the

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.