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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:06:46+00:00 2026-05-13T16:06:46+00:00

I have two classes that I would like to specify as follows: class Club

  • 0

I have two classes that I would like to specify as follows:

class Club < ActiveRecord::Base
  belongs_to :president, :class_name => "Person", :foreign_key => "president_id"
  belongs_to :vice_president, 
             :class_name => "Person",
             :foreign_key => "vice_president_id"
end

class Person < ActiveRecord::Base
  has_one :club, :conditions => 
  ['president_id = ? OR vice_president_id = ?', '#{self.id}', '#{self.id}']
end

This doesn’t work and gives me an error when trying to get the club association from the person object. The error is because is looking for person_id in the club table when I looked at the SQL. I can get around it by declaring multiple has_one associations, but feel like this is the improper way of doing it.

A person can only be the President or Vice President of one club.

Anyone able to offer a little bit of advice on this issue, I would be very appreciative.

  • 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-13T16:06:46+00:00Added an answer on May 13, 2026 at 4:06 pm

    Your has_one condition will never work in Rails, as far as I know.

    You need one explicit has_one or belongs_to or has_many per “link”, on both tables. So if you have two “links”, you need two has_one and two belongs_to. That is how it works.

    Secondly, I think you should reconsider your models. The way you are doing it, one person can not be the president of a club and an employee, at the same time. Or be the president of two clubs. Even if you don’t have these right now, they can come in the future – it is easier to stay flexible right now.

    A flexible way of doing this is using a has_many :through with an intermediate table that specifies the role. In other words:

    # The memberships table has a person_id, club_id and role_id, all integers
    
    class Membership < ActiveRecord::Base
      belongs_to :club
      belongs_to :person
      validates_presence_of :role_id
      validates_numericality_of :role_id
    end
    
    class Club < ActiveRecord::Base
      has_many :memberships, :dependent => :delete_all
      has_many :people, :through => :memberships
    end
    
    class Person < ActiveRecord::Base
      has_many :memberships, :dependent => :delete_all
      has_many :clubs, :through => :memberships
    end
    

    Now, assuming that role_id=0 means employee, role_id=1 means president, and role_id=2 means vice_president, you can use it like this:

    tyler = Person.find(1) # person_id is 1
    other = Person.find(2) # person_id is 2
    c = Club.find(1)  # club_id is 1
    
    tyler.clubs # returns all the clubs this person is "member" of
    c.people # returns all the "members" of this club, no matter their role
    
    #make tyler the president of c
    tyler.memberships.create(:club_id => 1, :role_id => 1)
    
    #make other the vicepresident of c
    #but using c.memberships instead of other.memberships (works both ways)
    c.memberships.create(:person_id => 2, :role_id => 1)
    
    #find the (first) president of c
    c.memberships.find_by_role_id(1).person
    
    #find the (first) vicepresident of c
    c.memberships.find_by_role_id(2).person
    
    #find all the employees of c
    c.memberships.find_all_by_role_id(0).collect { |m| m.person }
    
    #find all the clubs of which tyler is president
    tyler.memberships.find_all_by_role_id(1).collect { |m| m.club }
    

    Additional notes:

    • You could complement this with a roles table and model. Roles would have just a a name, roles would have_many relationships and memberships would belong_to role. Or, you could define methods in memberships for getting the role name (if 0, it returns “employee”, if 1, “president”, etc
    • You can add validations on memberhips so no more than 1 person can be made president of a given club, or the same employee on the same club twice. Later on, if you start getting “exceptional cases” in which a person needs to be in two places, you will just have to adapt your validations.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes that I would like to merge into a composite. These
I have two classes orchestrated by a main class and I would like to
I have two classes that I would like to transform to protobuf messages: [ProtoContract]
I have two classes, Phone.class and Time.class. I would like to use their methods
Suppose I have two classes a base class and an inherited class as follows:
Say I have two traits that I would like to mixin to a class.
I have two classes in my project that I would like to pass the
I have two classes that extend the activity class. Each class has it's own
VB.NET 2008 .NET 3.5 I have two base classes that are MustInherit (partial). Let's
I have two classes that don't know anything about themselfs class A, class B.

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.