I'm trying to set up an application that includes groups that can be composed of users or existing groups.
Examples:
- Users Andy, Bob, Charlie, and David are in the 1st forwarders group.
- Users Eddy, Frank, George, Howard, Iggy, and Jack are in the 1st midfielders group.
- Users Kenny, Lenny, Max, Ned, Oscar, Peter, and Quin are in the 1st defenders group.
- User Rita is the only user in the 1st goalkeeper group.
- Users Andy, Bob, Eddy, Frank, and Kenny are also in the alpha basketballers group.
- Groups 1st forwarders, 1st midfielders, 1st defenders, and 1st goalkeeper are in the 1st footballers group.
- Groups 1st forwarders, 2nd midfielders, 3rd defenders, and 4th goalkeeper are in the all-star footballers group.
As I understand it, this will require a polymorphic, self-referential set of models.
Based on this understanding, the three models and corresponding classes that I currently have are:
Users
- id: integer
- name: string
- plus other person-specific data
class User < ActiveRecord::Base
has_many :memberships, :as => :child
has_many :groups, :through => :memberships
end
Groups
- id: integer
- name: string
- plus other group-specific data
class Group < ActiveRecord::Base
# parental membership role
has_many :memberships
has_many :users, :through => :memberships
# child membership role
has_many :memberships, :as => :child
end
Memberships
- child_id: integer
- group_id: integer
- membership_type: string
- plus other membership-specific data
class Membership < ActiveRecord::Base
belongs_to :child, :polymorphic => true
belongs_to :group
end
When I try to access the child via something like Membership.first.child
I always get a => nil
response.
Do I have my models and classes set up correctly?
If not, what have I done incorrectly?
If so, how do I pull the child's information?
Or am I approaching this incorrectly, and if so, how should I approach it?