HABTM with conditional field in relation table

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’


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

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. :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs