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

  • Home
  • SEARCH
  • 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 9031399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:43:23+00:00 2026-06-16T07:43:23+00:00

I have the following structure: class User < ActiveRecord::Base end class Item < ActiveRecord::Base

  • 0

I have the following structure:

class User < ActiveRecord::Base
end

class Item < ActiveRecord::Base
end

class Group < ActiveRecord::Base
end

A User can create (and thereby own) a Group.
A Group is a list of Items and a list of the Users who can access these items.
In other words, a user has a list of Items and can control which Users can see these items through the Group membership.

How should I set up the relationship?

  • 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-16T07:43:24+00:00Added an answer on June 16, 2026 at 7:43 am

    Well, you’re going to run into some issues with the fact that you want a double many-to-many relationship here. Ie. groups have and belong to many items, and items have and belong to many users.

    So, I would setup the relationship this way, assuming you want a group to be able to have many items, and items may belong to more than one group:

    User has_many :groups
    User has_and_belongs_to_many :items
    User has_many :own_items, :class_name => 'Item'
    
    Group belongs_to :user
    Group has_and_belongs_to_many :items
    
    Item has_and_belongs_to_many :groups
    Item has_and_belongs_to_many :users
    Item belongs_to :owner, :class_name => 'User'
    

    Your migrations will need to look like so:

    # Group
    :user_id, :integer
    
    # Item
    :owner_id, :integer
    
    # GroupsItems
    :group_id
    :item_id
    
    #ItemsUsers
    :item_id
    :user_id
    

    Now, the structure you’re looking at isn’t the cleanest in the universe, but it will behave as you expect as long as you’re careful about the user association.

    For instance, to create a user’s item:

    @user = User.first
    @user.own_items.create(...)
    

    To assign users as able to view an item…

    @item = Item.find(...) #or @user.own_items.find(...)
    @item.users = [user1,user2,user3]
    

    Now, this sets up the relationships you want, but you’ll have to also write your own controller / view logic to limit access, or use a library like CanCan.

    For instance:

    # View
    - if @item.users.include?(current_user)
      ...show item... 
    
    # Items Controller:
    def show
      @item = Item.find(params[:id])
      if @item.users.include?(current_user)
        ...continue...
      else
        redirect_to :back, :alert => 'You are not authorized to view this item.'
      end
    end
    

    I hope those examples point you in the right direction. You’ll have a number of issues to deal with relating to access control, but trying to think of them and solve each one I can think of is beyond the scope of this question.

    Also, note that this is the simplest setup I could think of. If you have more complex logic in the associations you might want to make a full-fledged join model and use has_many :through associations instead of HABTM.

    Good luck!

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

Sidebar

Related Questions

I have the following model structure: class Group < ActiveRecord::Base has_many :group_products, :dependent =>
I have following structure: class User < ActiveRecord::Base has_many :Hobbies, :dependent => :destroy accepts_nested_attributes_for
I have the following structure: class User include Mongoid::Document end class Resource include Mongoid::Document
I have the following class structure: class Organization { string Name; List<User> users; List<Organization>
I have the following class structure class Top : NotifyPropertyChanged { private List<Inner> innerList;
I have a simple model class Ad < ActiveRecord::Base has_many :ad_items end class AdItem
I have the following model structure class User(db.Model) : nickname = db.StringProperty(required=True) fullname =
So I have the following model structure for a django project class RawData(models.Model): user
I have the following data structure: [DataServiceEntity] public class User { public string RowKey
I have the following two models: class Message < ActiveRecord::Base belongs_to :to_user, :class_name =>

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.