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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:48:12+00:00 2026-05-29T04:48:12+00:00

So this might be really bad form. I’m relatively new to rails. I’m not

  • 0

So this might be really bad form. I’m relatively new to rails. I’m not sure.

I have a project model and I want there to be many owners (who can read and write everything) and many collaborators (who can read and write some stuff).

In my project.rb file I have:

  has_many :project_user_relationships, :dependent => :destroy
  has_many :collaborators, :through => :project_user_relationships, :source => :user

  has_many :project_owners_relationships, :class_name => "ProjectUserRelationship", :foreign_key => "project_id", 
           :before_add => Proc.new { |p,owner_r| owner_r.owner = true }, :conditions => "`project_user_relationships`.owner = true"
  has_many :owners, :through => :project_owners_relationships, :source => :user

So this works reasonably well. If I add a new owner, that user is also a collaborator which is what I want. The issue I’m not sure how to solve is if I add a user that is already collaborator as an owner, I get two entries in the join table. I’d like for it to just amend the record that’s already there. How do I do that?

  • 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-29T04:48:13+00:00Added an answer on May 29, 2026 at 4:48 am

    Here’s the data model I would suggest for this:

    class Project < ActiveRecord::Base
        has_many :memberships, :dependent => :destroy
        ...
    end
    
    class Membership < ActiveRecord::Base
        belongs_to :project
        belongs_to :user
        ...
    end
    
    class User < ActiveRecord::Base
        has_many :memberships, :dependent => :destroy
        has_many :projects, :through => :memberships
        ...
    end
    

    And then the membership table will have the following attributes:

    :id
    :user_id
    :project_id
    :is_owner (boolean)
    

    A scope defined on the membership class:

    scope :owner, where("is_owner")
    

    And a special method for User instances:

    def owned_projects
        memberships.owner.includes(:projects).inject([]) {|array, m| array << m.project; array}
    end
    

    will allow you to retrieve a user’s owned projects with the user.owned_projects call.

    And just a call to user.projects to see a user’s projects that they either collaborate on or own.

    You have better data normalization with this data model, and a simple boolean attribute to define whether or not a user is a project owner.

    This data model is used in this project, with the exception that s/Project/Group/, and there’s some additional functionality to handle inviting users to the Project.

    This doesn’t answer your “real question”, but I think part of the issue is that a data model where collaborators are owners are stored in the same table is needed to minimize redundancies and the need to manage two separate tables.

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

Sidebar

Related Questions

I feel like this might be really simple but I'm just not getting it
I'm new to iPhone development and Xcode, so this might be really obvious. I
I'm new to C# so this might be a really dump question: I implemented
I know this might sound really ridiculas but all of a sudden when I
This might be a really trivial one. Is File storage OS dependant ? Why
This might be a really stupid question and make you guys laugh. I've deployed
This might be a really dumb question, however I've looked around online, etc. And
Ok, this might be a really odd problem. I am writing a web application,
This might be something very straight forward and I really think it should work
This might sound a silly question but I've really spend hours in trying to

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.