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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:54:33+00:00 2026-06-04T15:54:33+00:00

I have 4 models: SchoolClass , Timetable , Lesson , Reporting : # class_code

  • 0

I have 4 models: SchoolClass, Timetable, Lesson, Reporting:

#  class_code :string(255)
class SchoolClass < ActiveRecord::Base
  has_many :timetables
end

class Timetable < ActiveRecord::Base
  belongs_to :school_class
  has_many :lessons
end

class Lesson < ActiveRecord::Base
  belongs_to :timetable
  has_one :reporting
end

# report_type  :string(255)
class Reporting < ActiveRecord::Base
  belongs_to :lesson

  validates :report_type,
              :presence  => true,
              :inclusion => { :in => %w(homework checkpoint)}
end

How can i validate that each SchoolClass can have only 1 Reporting with type “checkpoint“?

  • 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-04T15:54:34+00:00Added an answer on June 4, 2026 at 3:54 pm

    This gets super complicated because of the nesting associations.
    I would start by using a custom validation method.

    in the SchoolClass model:

    validate :only_one_reporting_checkpoint
    

    and then the method:

    def only_one_reporting_checkpoint
      timetables = self.timetables
      reporting_checkpoint = nil
      timetables.each do |t|
        t.lessons.each do |l|
          reporting_checkpoint = true if l.reporting.report_type == "checkpoint"
        end
      end
      if reporting_checkpoint == true         
        errors.add(:reporting, "exception raised!")
      end
    end
    

    There, I think that does it. If I understand your problem correctly.

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

Sidebar

Related Questions

I have models: class User < ActiveRecord::Base has_many :user_skills end class UserSkill < ActiveRecord::Base
I have models: class Materialtitle < ActiveRecord::Base has_many :edocs end class Edoc < ActiveRecord::Base
I have 2 models: Video: class Video < ActiveRecord::Base belongs_to :user has_many :thumbnails attr_accessor
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
Let's say I have models that look like this: class Foo < ActiveRecord::Base has_many
I have models in my app: class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic =>
I have the following tables: class SchoolClass(models.Model): id = models.AutoField(primary_key = True) class_name =
Say I have models: class Animal(models.Model): type = models.CharField(max_length=255) class Dog(Animal): def make_sound(self): print
Right now I have: @models = ActiveRecord::Base.send(:subclasses) But I get this in the log:
I have models like this: class IdfPracownicy(models.Model): nazwa = models.CharField(max_length=100) class IdfPracaOpinie(models.Model): nazwa =

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.