has_many :through on self?

Posted by Glex on Stack Overflow See other posts from Stack Overflow or by Glex
Published on 2010-03-29T03:01:38Z Indexed on 2010/03/29 3:13 UTC
Read the original article Hit count: 495

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about rails