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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:43:16+00:00 2026-05-31T04:43:16+00:00

Here is my scenario: A model called Course has many CourseCodes. A CourseCode belongs

  • 0

Here is my scenario:

A model called Course has many CourseCodes. A CourseCode belongs to a Course.
A CourseCode can’t be created without Course and a Course can’t be created without at least one CourseCode.

class Course < ActiveRecord::Base
  has_many :course_codes
  validate :existence_of_code
private
  def existence_of_code
    unless course_codes.any?
      errors[:course_codes] << "missing course code"
    end
  end
end

class CourseCode < ActiveRecord::Base
  belongs_to :course
  validates_presence_of :course
end

The whole scenario feels a bit like catch 22.
Is there a way to create both on the same time?
I’m using Rails 3.2

  • 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-31T04:43:18+00:00Added an answer on May 31, 2026 at 4:43 am

    Solved the problem by using accepts_nested_attributes_for.

    Nested attributes allow you to save attributes on associated records through the parent. By default nested.

    class Course < ActiveRecord::Base
      has_many :course_codes, inverse_of: :course
      validate :existence_of_code
    
      accepts_nested_attributes_for :course_codes
    
    private
      def existence_of_code
        unless course_codes.any?
          errors[:course_codes] << "missing course code"
        end
      end
    end
    
    class CourseCode < ActiveRecord::Base
      belongs_to :course, inverse_of: :course_codes
      validates_presence_of :course
    end
    

    Used like this.

    Course.create!({
      course_codes_attributes: [{ code: "TDA123" }],
      # ...
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this simplified scenario, I have a model called Post Each post has a
I have a business model called Customer which has many required properties (via DataAnnotations)
Here is the scenario: I get an CSV file, which has data1, data2, data3,
Here's the scenario. There are two CoreData model objects, A and B, and the
I'm using Translate behavior. Here's the scenario: I save a translation on model 'Content'
Here's the scenario: I have a repeater inside an UpdatePanel called updPanel. Inside this
Here's the scenario... I have several float attributes in my data model which I
I have one scenario here, and maybe someone could enlighten me at this corner
Here is the senario. I have a model called Permission. Lets assume it looks
I know how to write tab host,but here the scenario is little bit different

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.