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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:44:30+00:00 2026-05-30T11:44:30+00:00

I have a weird case in my rails development that I’m not able to

  • 0

I have a weird case in my rails development that I’m not able to manage properly.
Basically, I have three objects: Domain, Project and Person ; a domain is a group of persons and projects. Domains can have several projects and projects can have several people however a project can only be in one domain and people can only work for projects in one domain.

I have represented it as following:

class Domain < ActiveRecord::Base
    has_many :projects

class Project < ActiveRecord::Base
    belongs_to :domain
    has_and_belongs_to_many :persons

class Person < ActiveRecord::Base
    belongs_to :domain
    has_and_belongs_to_many :projects

I don’t know how to validate that all the projects added to a person belongs to the same domain. I have created a method for validating persons however it is still possible to add projects in other domains, the person saved in database will just not be valid.

Do you see a clean solution to this problem?

  • 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-30T11:44:32+00:00Added an answer on May 30, 2026 at 11:44 am

    So, basically, you want to validate that a person takes projects only from one domain. I suppose this domain should be defined, meaning a person should have a domain_id column.

    You also have a many-to-many association, and, since the association needs some validations, you should have also a join model (instead of just a table without a model). I called it Work. So, I have this:

    class Domain < ActiveRecord::Base
      has_many :projects
    end
    
    class Project < ActiveRecord::Base
      belongs_to :domain
    
      has_many :works
      has_many :persons, :through => :works
    end
    
    class Work < ActiveRecord::Base
      belongs_to :project
      belongs_to :person
    end
    
    class Person < ActiveRecord::Base
      has_many :works
      has_many :projects, :through => :works
    end
    

    Now, to the Work model you just add

    validate :projects_belong_to_apropriate_domains
    
    def projects_belong_to_apropriate_domains
      if person.domain_id != project.domain.id
        errors[:base] << "A person may only take a project which belongs to his domain."
      end
    end
    

    This worked for me. Is this what you wanted?

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

Sidebar

Related Questions

I seem to have found a weird edge case where it seems that a
I have a weird date rounding problem that hopefully someone can solve. My client
I have a weird issue that only seems to be affecting IE 7. The
I have this weird crash that only happens when running the app on the
I have a really weird situation. The idea is there is a scroller that
I have a weird case here.... I'm making a simple magento module right now.
A weird case is happening in my application, some code that is in a
I have a bit of a weird use case where i need to change
I have a weird case here at work. The customer(telecommunication firm) has a server
I have three buttons on my main page. Something weird happens when I try

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.