【发布时间】:2011-08-02 12:49:21
【问题描述】:
我目前正在开发一个小型社交网络应用程序,现在我正在尝试创建一个表示用户之间友谊的模型。到目前为止,这是我想出的:
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
我的 friendship 模型有一个字段 confirmed 作为我想使用的布尔值 将友谊定义为未决或已确认。
如何访问某个用户的所有待处理请求?我可以以某种方式定义 这使用 Rails 的 scope 方法?类似的东西
current_user.friendships.requests # => [Friendship, Friendship, ...]
会很棒。
我怎样才能使这种关联是双向的?我是否只是添加另一个 友谊一旦有人确认了朋友请求,这样我的友谊 表看起来与此类似:
| user_id | friend_id | confirmed |
-----------------------------------
| 1 | 2 | true |
| 2 | 1 | true |
【问题讨论】:
-
朋友与其他用户的不同之处在于他们通过友谊联系在一起这一事实?
标签: ruby-on-rails ruby activerecord has-many-through