0

我按照本教程创建了一个BETA 邀请系统http://railscasts.com/episodes/124-beta-invitations。用户还可以在我的 Rails 应用程序中互相关注。

如何让受邀者关注邀请他注册的人?

目前,我正在尝试使用一种方法在我的用户模型中建立它,但是我无法创建允许被邀请者通过 sender_id/user_id 跟随邀请者的方法。

这是我到目前为止使用的代码。

架构

  create_table "users", :force => true do |t|
    t.string   "name"
    t.string   "email"
    t.integer  "invitation_id"
    t.integer  "invitation_limit"
    t.timestamp "created_at",                                :null => false
    t.timestamp "updated_at",                                :null => false
    t.string    "password_reset_token"
    t.timestamp "password_reset_sent_at"
  end

  create_table "invitations", :force => true do |t|
    t.integer  "sender_id"
    t.string   "recipient_email"
    t.string   "token"
    t.datetime "sent_at"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
  end

楷模

用户

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, :invitation_token

  has_many :relationships, foreign_key: "follower_id", dependent: :destroy
  has_many :followed_users, through: :relationships, source: :followed

  has_many :reverse_relationships, foreign_key: "followed_id",
                               class_name:  "Relationship",
                               dependent:   :destroy
  has_many :followers, through: :reverse_relationships, source: :follower

  has_many :sent_invitations, :class_name => 'Invitations', :foreign_key => 'sender_id'

  belongs_to :invitation

  after_create :follow_inviter      #---------- HERE!!

  def follow_inviter                #---------- HERE!!
    inviters = Invitation.find_by_sender_id
    inviters.each do |invite|
      self.follow!(invite)
    end
  end

  def invitation_token
    invitation.token if invitation
  end

  def invitation_token=(token)
    self.invitation = Invitation.find_by_token(token)
  end

  def following?(other_user)
    relationships.find_by_followed_id(other_user.id)
  end

 def follow!(other_user)
    relationships.create!(followed_id: other_user.id)
  end

  def unfollow!(other_user)
    relationships.find_by_followed_id(other_user.id).destroy
  end

end

关系

class Relationship < ActiveRecord::Base

  attr_accessible :followed_id

  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"

  validates :follower_id, presence: true
  validates :followed_id, presence: true
end

邀请函

class Invitation < ActiveRecord::Base
  attr_accessible :recipient_email, :sender_id, :sent_at, :token

  belongs_to :sender, :class_name => "User"
  has_one :recipient, :class_name => "User"

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  before_create :generate_token

  private

    def generate_token
      self.token = Digest::SHA1.hexdigest([Time.now, rand].join)
    end

end
4

3 回答 3

1

这应该有效。

def follow_inviter
  if invitation = Invitation.find_by_recipient_email(email)
    follow!(invitation.sender)
  end
end

但是您的模型关联定义不明确。例如has_one :recipient, :class_name => "User",在Invitation期望表中有 a recipient_id,但没有。你应该回顾一下。

于 2013-09-14T08:16:36.987 回答
1

我可能错了,我是一名初级 Rails 开发人员,但看起来你在哪里inviters = Invitation.find_by_sender_id应该有这样的东西,发送邀请的人的 id 是inviters = Invitation.find_by_sender_id(id_of_sender)哪里。id_of_sender

需要 1 个参数(要找到的发件人的find_by_sender_idID),这就是您收到错误的原因wrong number of arguments (0 for 1)

同样对于它的价值,我很确定这些find_by_*_id方法(其中 * 是数据库中的模型)已被弃用。Rails 4 使用类似Invitation.find(id_of_sender). 当您find在活动记录模型上调用该方法时,它需要一个 id 作为参数。

您还可以使用Invitation.find_by(email: 'user@example.com')根据您提供的任何属性来查找记录。

于 2013-09-14T05:44:37.690 回答
0

抱歉,如果我误解了您的问题...我有类似的要求,一个用户可以将另一个用户标记为“最喜欢”的用户(即用户表具有自我关系)

为了实现这一点,我添加了一个表,user_favorite如下所示:

db\schema.rb

  create_table "user_favorites", :id => false, :force => true do |t|
    t.integer "user_id"
    t.integer "favorite_user_id"
  end

  add_index "user_favorites", ["user_id"], :name => "index_user_favorites_on_user_id"

app\models\user.rb

class User < ActiveRecord::Base
  has_and_belongs_to_many       :favorite_users, :class_name => 'User', :join_table => "user_favorites", :foreign_key => "user_id", :association_foreign_key => "favorite_user_id"

  def is_favorite?(user)
    self.favorite_users.include?(user)
  end

  def toggle_favorite(favorite_user)
    if favorite_user
      if self.favorite_users.include?(favorite_user)
        self.favorite_users.delete favorite_user
      else
        self.favorite_users << favorite_user
      end
    end  
  end
end
于 2013-09-14T06:16:13.593 回答