我目前正在研究一個小型社交網絡應用程序,現在我正在嘗試創建一個代表友誼的模型之間的用戶。這是我想出了迄今:如何在社交應用程序的Rails 3中實現友誼模型?
class User < ActiveRecord::Base
# ...
has_many :friendships
has_many :friends, :through => :friendships
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => 'User'
end
我友誼模型已經確認因爲我想用 定義友誼未決或證實其布爾場。
如何訪問某個用戶的所有待處理請求?我可以以某種方式定義 這使用Rails的範圍方法嗎?像
current_user.friendships.requests # => [Friendship, Friendship, ...]
會很好。
如何使此關聯雙向?我簡單地添加其他 友誼一旦一個已經確認朋友的要求,使得我的友誼 表將類似於此:
| user_id | friend_id | confirmed |
-----------------------------------
| 1 | 2 | true |
| 2 | 1 | true |
除了友情鏈接之外,朋友與其他用戶有什麼不同? – 2011-04-10 17:06:10