Forum: Ruby on Rails HABTM with conditional field in relation table

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joshua M. (Guest)
on 2006-01-12 11:49
Hi all

I have members in my application, and every member can assign other
members as buddies.

class Member < ActiveRecord::Base
  # Hat folgende Buddies
  has_and_belongs_to_many :buddies,
                          :class_name => 'Member',
                          :join_table => 'members_have_buddies',
                          :association_foreign_key => 'buddy_id',
                          :order => 'username'

  # Ist Buddy von
  has_and_belongs_to_many :buddy_of,
                          :class_name => 'Member',
                          :join_table => 'members_have_buddies',
                          :foreign_key => 'buddy_id',
                          :order => 'username'
  ...
end

Now I want to extend this feature, that buddies aren't automatically
allowed, but when a member wants to add somebody as buddy the added
member has to give his confirmation first.

I added the field accepted to the relation table, now it looks like
that:

members_have_buddies(member_id, buddy_id, accepted)

But how can I implement now, that I have the following...?

josh $ ./script/console
josh $ m = Member.find(1)
josh $ m.buddies # should give an array of only the buddies that have
accepted (actually gives a list of all the member's buddies atm)
josh $ m.buddies_waiting_for_acceptance # should give an array of only
the buddies that I didn't accept yet
josh $ m.buddies_you_dont_accept # should give an array of buddies of
whom you don't want to be added as buddy
josh $ m.buddies_that_dont_accept_you # should give an array of buddies
that don't accept you as buddy

I want to have the following code numbers in the accepted field:

0 - waiting for acceptance
1 - accepted
2 - not accepted

But now how can i realize this? Should I create again HABTM dependencies
using the :conditions key? Or what would you suggest?

Thanks a LOT for help. :-)
Joshua
This topic is locked and can not be replied to.