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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:29:17+00:00 2026-05-25T13:29:17+00:00

I am trying to extract all Posts for a given User in the bellow

  • 0

I am trying to extract all Posts for a given User in the bellow relationships. Not sure whether I got them right, so I’ll better explain. A User has Ownerships and Memberships in some Groups. A User can be either a Member or an Owner of the Group, but not both. Every Post has an id of the user and of the group. I think the problem is due to the relationships noted below. How can I get around it? One more thing. I have to also find all posts that were posted by other users in the user’s groups. In other words, I have to pass through groups.

       /-- Owner ---\
User --              -- Group -- Post
  |    \-- Member --/             |
  |_______________________________|

class User < ActiveRecord::Base
  has_many :posts, :dependent => :destroy
  has_many :ownerships, :foreign_key => "user_id", :dependent => :destroy
  has_many :memberships, :foreign_key => "user_id", :dependent => :destroy

  # Problem with these two? I think so.
  has_many :groups, :through => :memberships, :source => :user
  has_many :groups, :through => :ownerships, :source => :user

class Ownership < ActiveRecord::Base
  belongs_to :users, :class_name => "User"
  belongs_to :groups, :class_name => "Group"
  has_many :posts, :through => :groups, :source => :posts

class Membership < ActiveRecord::Base
  belongs_to :users, :class_name => "User"
  belongs_to :groups, :class_name => "Group"
  has_many :posts, :through => :groups, :source => :posts

class Group < ActiveRecord::Base
  has_many :posts, :dependent => :destroy

class Post < ActiveRecord::Base
  belongs_to :user
  belongs_to :groups

The errors is coming from the line:

_groups = user.groups

The error as following:

Could not find the source association(s) :user in model Ownership. Try ‘has_many :groups, :through => :ownerships, :source => ‘. Is it one of :users, :groups, or :postings?

  • 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-25T13:29:17+00:00Added an answer on May 25, 2026 at 1:29 pm

    First up: you’re getting that error you’re seeing because you’ve defined the associations in the Membership and Ownership table as this:

    belongs_to :users
    

    When they should belong to only one user, i.e. singular user:

    belongs_to :user
    

    But even then you will run into problems!

    I think having a Membership model and an Ownership model are what will trip you up next. I don’t understand what the purpose of having an Ownership model provides, other than signifying ownership of a group, which could be done by a field on the memberships table’s records called owner for instance. It’s over-engineering.

    The problem with the Rails code you’ve got there is that you’re defining that you have many posts through one association and then you’re telling it that you have many posts through another association. In effect, you’re doing this:

     def posts
       # find posts for the groups that I own
     end
    
     def posts
       # find posts for the groups I belong to
     end
    

    It is not mistake here that there are two identically-named methods. This is exactly what you are doing by defining two has_many associations with the same name.

    So hopefully now you can see why having an Ownership and a Membership model is the path to madness.

    I would really recommend that you just have a Membership model that has a boolean attribute declaring an owner for a group. This would also mean that, if you wanted to, you could have new owners for a group in a very easy fashion: just flip the boolean. No need to create another record in another table.

    One Membership model to rule them all.

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

Sidebar

Related Questions

I'm trying to extract all matches from a EBML definition, which is something like
I am trying to extract all the <input > tags out of a <form>
What I am trying to achieve is to extract all links with a href
Trying to extract files to a given folder ignoring the path in the zipfile
I've spent ages on this, all I'm trying to do is extract the title
I am trying to extract all substrings in a string that is between the
I have a list of files and I'm trying to extract all layer1_*.grd files.
I am trying to extract all links that have /thumb/ in it within 's.
I'm trying to extract all rows from a datatable where CODE follows the pattern
I'm trying to open a word document and just extract all the text that

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.