如何实现在Rails 3的友谊模式的社交网络应用程序? [英] How to Implement a Friendship Model in Rails 3 for a Social Networking Application?

查看:150
本文介绍了如何实现在Rails 3的友谊模式的社交网络应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个小的社交网络应用程序,现在我想创建一个重新的用户的之间presents的的友谊的典范。这是我想出了至今:

 类用户的LT;的ActiveRecord :: Base的

  #...
  的has_many:友谊
  的has_many:朋友:通过=> :友谊

结束

一流的友谊<的ActiveRecord :: Base的
  belongs_to的:用户
  belongs_to的:朋友:将class_name => '用户'
结束
 

我的友谊的模型有一个字段的确认的布尔我想使用 定义一个友谊为未完成或者证实。

我如何可以访问某个用户所有待处理的请求?我能以某种方式定义 这种使用Rails的范围的方法?类似于

  current_user.friendships.requests#=> [友谊,友谊,...]
 

将是巨大的。

我怎样才能让这种关联双向?难道我只是添加其他 一旦人们的友谊已经确认朋友的要求,使得我的友谊 表将类似于此:

  | USER_ID | friend_id |确认|
-----------------------------------
| 1 | 2 |真|
| 2 | 1 |真|
 

解决方案

要访问所有悬而未决的友谊,你可以使用一个关联:

 的has_many:pending_friends,
         :通过=> :友谊,
         :源=> :朋友,
         :条件=> 确认= 0#假设0表示待定'
 

要做出的友谊双向的,你可能需要更换您的布尔的确认的列用字符串的状态的,有以下三个值中的一列:待定, '请求的'和'接受'(任选'拒绝')。这将有助于保持跟踪谁做的友谊请求。

在一个交友请求发送(从富说吧),将创建两个交情记录(封装在一个事务):有一个请求,一个悬而未决反映RESP。该酒吧已经从foo的请求友谊与富与酒吧挂起的友谊。

 高清self.request(用户,朋友)
    除非用户==朋友或Friendship.exists?(用户,朋友)
      交易做
        创建(:用户=>用户,:朋友=>朋友:状态=>悬而未决)
        创建(:用户=>朋友:朋友=>用户,:状态=>'要求')
      结束
    结束
  结束
 

在友谊被接受(由律师EG),这两个友好的记录都设置为接受。

 高清self.accept(用户,朋友)
    交易做
      accepted_at = Time.now
      accept_one_side(用户,朋友,accepted_at)
      accept_one_side(朋友,用户,accepted_at)
    结束
  结束

  高清self.accept_one_side(用户,朋友,accepted_at)
    请求= find_by_user_id_and_friend_id(用户,朋友)
    request.status ='接受'
    request.accepted_at = accepted_at
    request.save!
  结束
 

这主要是包括在Railspace本书由迈克尔·哈特尔和奥勒留Prochazka第14章。这里的源$ C ​​$ C 这应有助于您优化的解决方案。

I'm currently working on a small social networking application and right now I'm trying to create a model that represents friendships between users. This is what I came up with so far:

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

My friendship model has a field confirmed as boolean which I'd like to use to define a friendship as pending or confirmed.

How can I access all pending request for a certain user? Can I somehow define this using Rails' scope method? Something like

current_user.friendships.requests # => [Friendship, Friendship, ...]

would be great.

How can I make this association bidirectional? Do I simply add another friendship once one has confirmed a friend request such that my friendship table would look similar to this:

| user_id | friend_id | confirmed |
-----------------------------------
| 1       | 2         | true      |
| 2       | 1         | true      |

解决方案

To access all pending friendships you can use an association:

has_many :pending_friends,
         :through => :friendships,
         :source => :friend,
         :conditions => "confirmed = 0"  # assuming 0 means 'pending'

To make the friendship bidirectional, you may want to replace your boolean confirmed column with a string status column that has one of the following three values: 'pending', 'requested' and 'accepted' (optionally 'rejected'). This will help keep track of who made the friendship request.

When a friendship request is sent (say from Foo to Bar), you create two friendship records (encapsulated in a transaction): one requested and one pending to reflect resp. that Bar has a requested friendship from Foo and Foo has a pending friendship with Bar.

  def self.request(user, friend)
    unless user == friend or Friendship.exists?(user, friend)
      transaction do
        create(:user => user, :friend => friend, :status => 'pending')
        create(:user => friend, :friend => user, :status => 'requested')
      end
    end
  end

When the friendship is accepted (e.g. by Bar), both friendship records are set to accepted.

  def self.accept(user, friend)
    transaction do
      accepted_at = Time.now
      accept_one_side(user, friend, accepted_at)
      accept_one_side(friend, user, accepted_at)
    end
  end

  def self.accept_one_side(user, friend, accepted_at)
    request = find_by_user_id_and_friend_id(user, friend)
    request.status = 'accepted'
    request.accepted_at = accepted_at
    request.save!
  end

This is largely covered in chapter 14 of the Railspace book by Michael Hartl and Aurelius Prochazka. Here's the source code which should help you refine your solution.

这篇关于如何实现在Rails 3的友谊模式的社交网络应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆