2

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?

A Lion
  • 145
  • 1
  • 8
  • How are you saving this membership model? And where are the polymorphic association columns? – Maurício Linhares Aug 20 '11 at 11:52
  • @Mauricio - Currently I'm just using the console to manually create, save, and view the models' data. As I understand it, within the Memberships model the `child_id` and `membership_type` are the polymorphic association columns, with the `belongs_to :child, :polymorphic => true` in the Membership class and the `has_many :memberships, :as => :child` in the `User` and `Group` classes providing the other polymorphic information. Of course, since I can't currently access a group's child information, I'm assuming I've messed something up. ;) – A Lion Aug 20 '11 at 16:49
  • Yes, you did, the polymorphic association columns should be named **child_id** and **child_type**. – Maurício Linhares Aug 20 '11 at 16:51
  • @Mauricio - _Thank you!_ I clearly misunderstood several of the examples I reviewed and as a result hadn't even tried something so obvious. Would you please post your last comment as the answer. – A Lion Aug 20 '11 at 16:59

1 Answers1

1

The polymorphic association columns should be child_id and child_type.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158