Hi,
I’ve implemented a friends relationship in my user model (courtesy of
Chad F., Rails Recipies) and it has worked well, until I needed to
change the order of list of friends.
Here’s the use model:
has_and_belongs_to_many :friends,
:class_name => “User”,
:join_table => “friends_users” ,
:association_foreign_key => “friend_id”,
:foreign_key => “user_id”,
:order => 'weight DESC ’ # <------------------IMPORTANT
Here’s the Table:
mysql> desc friends_users;
±----------±--------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±----------±--------±-----±----±--------±------+
| user_id | int(11) | YES | | NULL | |
| friend_id | int(11) | YES | | NULL | |
| weight | int(11) | YES | | 0 | |
The idea here is that they ‘weight’ field will track the ‘degree of
friendship’.
I’d like to display a users list of friends in descending order of the
‘weight’
The good news is that is all works…but not in real time.
For other business reasons, I have my session data lasting for 2 weeks.
Thus, it is not refreshed with every logout. User interactions update
this weighting, and thus the cached session info becomes out of date.
The bottom line is that I’d like to stimulate the update
@session[‘user’].friends array in a very clean ‘railsy’ way.
Any thoughts?