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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:42:14+00:00 2026-05-27T14:42:14+00:00

A Course has many Lessons , and they are chosen by the user with

  • 0

A Course has many Lessons, and they are chosen by the user with a JS drag-n-drop widget which is working fine.

Here’s the relevant part of the params when I choose two lessons:

Parameters: {
  "course_lessons_attributes"=>[
    {"lesson_id"=>"43", "episode"=>"1"}, 
    {"lesson_id"=>"44", "episode"=>"2"}
  ]
}

I want to perform some validations on the @course and it’s new set of lessons, including how many there are, the sum of the lessons’ prices and other stuff. Here’s a sample:

Course Model

validate :contains_lessons
def contains_lessons
  errors[:course] << 'must have at least one lesson' unless lessons.any?
end

My problem is that the associations between the course and the lessons are not yet built before the course is saved, and that’s when I want to call upon them for my validations (using course.lessons).

What’s the correct way to be performing custom validations that rely on associations?

Thanks.

  • 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-27T14:42:15+00:00Added an answer on May 27, 2026 at 2:42 pm

    You can’t access the course.lessons, but the course_lessons are there, so I ended up doing something like this in the validation method to get access to the array of lessons.

    def custom validation
      val_lessons = Lesson.find(course_lessons.map(&:lesson_id))
      # ...
      # check some things about the associated lessons and add errors 
      # ...
    end
    

    I’m still open to there being a better way to do this.

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

Sidebar

Related Questions

I have a data model in Doctrine/symfony. I have a 'Course' which has many
Here is my scenario: A model called Course has many CourseCodes. A CourseCode belongs
I have a User model which has many projects and a Project model which
I am working for a project which has many XSLT transformations. The transformations have
I have a simple question. Of course it has many answers but I cannot
Do you know any university which has its C++ course available online? I'm looking
I maintain an application which, during the course of two years, has constantly required
I have a table Course and every Course has many Resources. Course ========== course_id
I have an SVN repository, which has many directories inside. Now I would like
so first my model structure: company has many users, user belongs to company model1

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.