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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:07:02+00:00 2026-05-17T22:07:02+00:00

I’m pretty new to database setup in Ruby and need help in setting it

  • 0

I’m pretty new to database setup in Ruby and need help in setting it up correctly along with the models. Can anyone help?

Basically I have an Organisation, which will have at least 2 types of Users – Member and Admin User. Both the Organisations and the Users have an Address.

I was thinking this was basically three tables – Organisation, User and Address, but then got really confused when trying think about models and foreign keys.

Can anyone suggest the best way to organise this?

I’m running Rails 3 with a mySql database.

Thanks for your time

Sniffer

  • 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-17T22:07:03+00:00Added an answer on May 17, 2026 at 10:07 pm

    I like a lot of Adam Tanner’s answer, but I would set it up a little differently. First, the way an Organization associates with admins doesn’t work as described – you’d have to have a different foreign key in your user table, and specify that in the has_one :admin association. But I don’t think that’s a good path anyway, because it limits you to one admin per organization, and limits a user belonging to one organization.

    My version is slightly more complicated, but I think it gets the job done well. First, admin should be a role that a user has or doesn’t have with an organization. I’ll address the user/org issue first, and save the address issue for later.

    Here are the migrations, which you can enhance with whatever other fields they need:

    create_table :organizations do |t|
      # your fields go here
    end
    
    create_table :users do |t|
      # your fields go here
    end
    
    create_table :memberships do |t|
      t.integer :user_id
      t.integer :organization_id
      t.boolean :is_admin
    end
    
    add_index :memberships, [:user_id, :organization_id]
    

    As you can see, we’re adding a memberships table, which is going to connect users and organizations. We also add an index to speed up the association a little. Now for the models:

    class Organization < ActiveRecord::Base
      has_many :memberships
      has_many :users, :through => :memberships
    end
    
    class User < ActiveRecord::Base
      has_many :memberships
      has_many :organizations, :through => :memberships
    
      def membership_in organization
        self.memberships.detect{|m| m.organization = organization}
      end
    
      def is_admin_for? organization
        self.membership_in(organization).is_admin?
      end
    
      def set_admin_for organization, value
       self.membership_in(organization).update_attribute(:is_admin, value)
      end
    end
    
    class Membership < ActiveRecord::Base
      belongs_to :organization
      belongs_to :user
    end
    

    Here, we’re connecting our users and organizations through memberships. A user can be an admin for any of the organizations they belong to. I’ve created a few methods to set and get the admin status of a user in an organization, in the user model.

    Next the addresses: I’ve already tackled this one in a blog post of mine:

    http://kconrails.com/2010/10/19/common-addresses-using-polymorphism-and-nested-attributes-in-rails/

    If you have any questions, please ask. Good luck!

    UPDATE

    Edward M. Smith pointed out in the comments that my admin methods aren’t very fault-tolerant. I was trying to keep the code as clean as possible for the example, but he has a point. So here’s the beefier version that accounts for trying to use a membership in an organization the user isn’t part of:

      def is_admin_for? organization
        membership = self.membership_in(organization)
        return false if membership.nil?
    
        membership.is_admin?
      end
    
      def set_admin_for organization, value
        membership = self.membership_in(organization)
        return false if membership.nil?
    
       membership.update_attribute(:is_admin, value)
      end
    

    As always, test-driven development is best, but I usually don’t have the time to do that for stackoverflow questions 🙂

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

Sidebar

Related Questions

No related questions found

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.