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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:33:38+00:00 2026-06-15T17:33:38+00:00

I have an Assignment model which has the following fields: :user_id , :company_id ,

  • 0

I have an Assignment model which has the following fields: :user_id, :company_id, :role_id

Each user can have multiple roles within a company but each company can only have one user with role_id == 5.

I would like to do something like below (obviously this does not work).

validates :company_id, :uniqueness => { :scope => :role_id => {:is => 5}, :message => "Only one owner is allowed." }

Is there a way to achieve this in a similar format to the above or will I need to create a custom validation macros?

If a custom validation is required how exactly would this be done and where is the ‘correct’ place to store this code if a new class needs to be created that inherits from ActiveModel::EachValidator? (Do I keep it in the same file or create a new file and possibly save it in /lib?)

Thanks in advance.

  • 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-15T17:33:38+00:00Added an answer on June 15, 2026 at 5:33 pm

    Ok, I’ve come up with a solution but not quite sure if it’s the ‘rails way’ of doing this or if it’s just a hack-y workaround. Can someone please correct me on this?

    I have done the following in my Association.rb file:

    validate :owner_exists
    .
    ..
    ...
    protected
      def owner_exists
          errors.add(:role_id, " of owner already exists") if (role_id == 5 && Assignment.where('company_id = ? AND role_id = 5', company_id ).exists?)
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an assignment in which I have to allow a user to plot
I currently have a model for an assignment table in Rails 3, which looks
I have an Assignment class which belongs to both a User class and a
I have the following three models: User, Project, and Assignment. A User has_many Projects
I have an app with the following models: User , Task , and Assignment
I have the following models to associate users with roles: class User < ActiveRecord::Base
I'd like to have the following. Imagine you have a homework assignment that has
So in my model a user has many roles through assignments and a role
For a database assignment I have to model a system for a school. Part
I am developing a rails application in which I have two models User and

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.