views:

35

answers:

1

I have a User model. A user can be either a dj, a club, or a clubber (this is controlled by the User#account_type attribute).

A club can have many djs, and a dj can have many users:

enumerated_attribute :account_type, %w(^clubber dj club), :nil => false do
  label :clubber => "Clubber"
  label :dj => "DJ"
  label :club => "Club"
end

has_many :dj_club_relationships, :class_name => "User", :dependent => :destroy
has_many :dj_user_relationships, :dependent => :destroy

has_many :djs, :through => :dj_club_relationships, :class_name => "User"
has_many :users, :through => :dj_user_relationships

However, this doesn't work as well as expected, since Rails doesn't know, for example, that it needs to destroy all dj_club_relationships with club_id when the user being destroyed is a club, and with dj_id when the user is a dj.

How can I help rails know about it?

A: 

Seems like one alternative is the dreaded STI (single table inheritance) which would let you handle the differences between accounts but also allow you to model the club and user relationships independently.

John Paul Ashenfelter
Yeah, but STI would require any search on all users (one of the most frequent operations) to search in 3 tables!Is there another way?
glebm