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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:39:48+00:00 2026-05-23T06:39:48+00:00

Ok so here goes. I don’t know if I’m over complicating things or if

  • 0

Ok so here goes. I don’t know if I’m over complicating things or if I’m just still so new to Rails that I don’t understand the basics. What I want in sudo code is this:

User
has_many projects as owner through relationship
has_many projects as contributor through relationship
has_many projects as follower through relationship

Project
has_one user as owner through relationship
has_many users as followers through relationship
has_many users as contributors through relationship

Relationship
belongs_to user
belongs_to project

Then I’m wanting to have the following magical things:

owner = Project.owner
followers = Project.followers
contributors = Project.contributors

projects = User.projects
myprojects = User.projects... (now I'm really not sure)
followedProjects = ...
contributingProjects = ...

So in writing that down I can see that there is another gap in my understanding of this model. The users can have the role of owner, follower or contributor or any combination of all three.

In terms of real code I have added here what I think is the relevant parts:

class User < ActiveRecord::Base
  has_many :user_project_relationships, :as => :relateable, :class_name => "UserProjectRelationship"
  has_many :projects, :as => :owner, :through => :relateable, :class_name => "Project", :source_type => :owner
  has_many :projects, :as => :follower, :through => :relateable, :class_name => "Project", :source_type => :follower
  has_many :projects, :as => :contributor, :through => :relateable, :class_name => "Project", :source_type => :contributor
end

class Project < ActiveRecord::Base
  has_many :user_project_relationships, :as => :relateable, :class_name => "UserProjectRelationship"
  has_one :user, :as => :owner, :through => :relateable, :class_name => "User"
  has_many :users, :as => :followers, :through => :relateable, :source_type => :follower, :class_name => "User"
  has_many :users, :as => :contributors, :through => :relateable, :source_type => :contributor, :class_name => "User"
end

class UserProjectRelationship < ActiveRecord::Base
  belongs_to :user
  belongs_to :project, :polymorphic => true
end

The migration for the relationships model is:

class CreateUserProjectRelationships < ActiveRecord::Migration
  def self.up
    create_table :user_project_relationships do |t|
      t.integer :relateable_id
      t.string :relateable_type
      t.integer :project_id
      t.timestamps
    end
    add_index :user_project_relationships, [:relateable_id, :relateable_type], :name => :relateable
    add_index :user_project_relationships, :project_id
  end

  def self.down
    drop_table :user_project_relationships
  end
end

Currently I get errors for things like project.users ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :relateable in model Project

I feel like I’m too in the wilderness here to really get what I want, and maybe relying on magical rails to do more than it does. Any guidance on the best path would be greatly appreciated.

Thanks in advance

Steve

  • 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-23T06:39:49+00:00Added an answer on May 23, 2026 at 6:39 am

    Rails can do alot, but I think instead you’re trying to make the Relationship model do too much. Each of those are a different kind of relationship, so I think try to keep them so.

    Split that up into separate join models:

    class User < ActiveRecord::Base
    
      has_many :owned_projects, :class_name => "Project", :foreign_key => :owner_id
    
      has_many :projects_followers
      has_many :followed_projects, :class_name => "Project", :through => :projects_followers
    
      has_many :projects_contributors
      has_many :contributed_projects, :class_name => "Project", :through => :projects_contributors
    
    end
    
    class Project < ActiveRecord::Base
      belongs_to :owner
    
      has_many :projects_followers
      has_many :followers, :class_name => "User", :through => :projects_followers
    
    
      has_many :projects_contributors, :foreign_key => :contributor_id
      has_many :contributors, :class_name => "User", :through => :projects_contributors
    
    end
    
    class ProjectsFollowers < ActiveRecord::Base
      belongs_to :user
      belongs_to :project
    end
    
    class ProjectsContributors < ActiveRecord::Base
      belongs_to :user
      belongs_to :project
    end
    

    Should be a lot closer to what you want. You can then indeed do

    project.owner
    project.followers
    project.contributors
    

    and

    user.owned_projects
    user.followed_projects
    user.contributed_projects
    

    That should either work, or get you pretty close.

    I think your mixup came from trying to make a polymorphic relationship, which I don’t think is desireable here. AFAI grok, the use case for polymorphic relationships is when you want 1 Model to be related to Any number of other models in the same way. That’s not the case here, as you have 2 models with 3 different types of relationships between them.

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

Sidebar

Related Questions

I just found some... I don't know what I'd call it but here goes:
Ok so this maybe a simple/silly question but I don't know so here goes:
I don't know if this is possible in Linq but here goes... I have
I don't know exactly the cause of this symptom but here goes. In our
I don't know what this is called but here goes. public class Person {
I don't know if this is strictly a programming question but here goes. I
I don't know if this is possible in python but here goes ... I
I don't know if this even is possible, but here goes, how do you
I don't even know how to describe what I want, but here goes. Assume
I don't know if this is even possible, but here goes: I have an

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.