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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:12:50+00:00 2026-05-28T01:12:50+00:00

I have several models: User = an ordinary user class (does not belong to

  • 0

I have several models:

  • User = an ordinary user class (does not belong to anything)
  • Challenge = any sort of competition (does not belong to anything)
  • Team = a list of users (belongs to Challenge)
  • Coach = name and role of a coach, which can be shared between several teams, but exists only for a given Challenge (belongs to Challenge)
  • TeamCoach = an association between Team and Coach (belongs to Team and Coach)

I want to make sure that it is impossible to have a TeamCoach with a Team and a Coach that do not belong to the same Challenge.

My current (working) implementation is the following:

class TeamCoach < ActiveRecord::Base
  attr_readonly :coach_id, :team_id

  belongs_to :coach
  belongs_to :team

  validates :coach_id, :presence => true,
                       :uniqueness => { :scope => :team_id }

  class SameChallengeValidator < ActiveModel::Validator
   def validate(team_coach)
     if team_coach.team.challenge_id != team_coach.coach.challenge_id
       team_coach.errors[:base] << "The team and coach do not belong to the same challenge."
     end
   end
  end 

  validates_with SameChallengeValidator
end

Is there a shorter, more elegant way to do the SameChallengeValidator validation?

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-28T01:12:51+00:00Added an answer on May 28, 2026 at 1:12 am

    You don’t really need to write your own validator class. You can just use the validate method instead:

    class TeamCoach < ActiveRecord::Base
      attr_readonly :coach_id, :team_id
    
      belongs_to :coach
      belongs_to :team
    
      validates :coach_id, :presence => true,
                           :uniqueness => { :scope => :team_id }
      validate :team_and_coach_belong_to_same_challenge
    
      private
       def team_and_coach_belong_to_same_challenge
         errors.add(:base, "The team and coach do not belong to the same challenge.") if self.team.challenge_id != self.coach.challenge_id
       end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 models: class Team(models.Model): Team model for creating a basic team. image
I have several Models: Workspace User Asset WorkspaceUser WorkspaceAsset Workspace has many users and
I have two models, Picture and SubmittedPicture as follows: class Picture(models.Model): user = models.ForeignKey(User)
Given a model structure like this: class Book(models.Model): user = models.ForeignKey(User) class Readingdate(models.Model): book
I've got some models set up like this: class AppGroup(models.Model): users = models.ManyToManyField(User) class
I dont see why it's not working. I have created several databases and tables
Let's say I have the following model: class Contest: title = models.CharField( max_length =
I have a project with several models like students,workers,administrators.... Of course those get different
I have 2 models: User and Interest. To get a specific users interest, you
I have several models that are related to one another through a HABTM relationship.

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.