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?
First up: you’re getting that error you’re seeing because you’ve defined the associations in the
MembershipandOwnershiptable as this:When they should belong to only one user, i.e. singular
user:But even then you will run into problems!
I think having a
Membershipmodel and anOwnershipmodel are what will trip you up next. I don’t understand what the purpose of having anOwnershipmodel provides, other than signifying ownership of a group, which could be done by a field on themembershipstable’s records calledownerfor 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:
It is not mistake here that there are two identically-named methods. This is exactly what you are doing by defining two
has_manyassociations with the same name.So hopefully now you can see why having an
Ownershipand aMembershipmodel is the path to madness.I would really recommend that you just have a
Membershipmodel 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
Membershipmodel to rule them all.