【问题标题】:Dont Follow Inviter on SIgnUp if Admin User如果管理员用户,请勿在 SIgnUp 上关注邀请者
【发布时间】:2013-09-16 03:13:28
【问题描述】:

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

目前我有两种方法:

  1. 允许用户在注册时关注所有管理员用户。

  2. 允许用户在注册时关注 INVITER。

我遇到的问题是:

no such column: TRUE: SELECT DISTINCT "users".* FROM "users" INNER JOIN 
"invitations" ON "invitations"."id" = "users"."invitation_id" WHERE
(invitations.recipient_email = 'spedroza@usc.edu' OR users.admin IS TRUE)

是否有办法解决第二种方法,如果他是 ADMIN USER,它就不会跟随邀请者?

型号

用户

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_and_admins      #--------HERE!!

  def follow_inviter_and_admins               #--------HERE!!
     return true if admin?
     users = User.joins(:invitation).where('invitations.recipient_email = ? OR users.admin IS admin', email).uniq
     users.each do |user|
       self.follow!(user)
     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

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"

  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

架构

create_table "users", :force => true do |t|
    t.string   "name"
    t.string   "email"
    t.integer  "invitation_id"
    t.integer  "invitation_limit"
    t.boolean  "admin",                  :default => false   #----------HERE!!
    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

【问题讨论】:

    标签: ruby-on-rails ruby methods admin


    【解决方案1】:

    例如,您可以在表 users 中找到所有唯一值。

    # user.rb
    
    after_create :follow_inviter_and_admins
    
    def follow_inviter_and_admins
      return true if admin?
      sender_id = Invitation.where(recipient_email: email).first.try(:sender_id)
      users = User.where('id = ? OR users.admin = ?', sender_id, true).uniq
      users.each do |user|
        self.follow!(user)
      end
    end
    

    这样的事情应该从表users中选择不同的值,它们是管理员或邀请者。因为值是唯一的,所以错误应该消失了

    【讨论】:

    • 如果我正确地理解了您的想法,您希望为管理员跳过此回调。所以你可以在回调unless: :admin?details之后添加一个选项。当然用户应该响应这个方法!或者您可以在方法follow_inviter_and_admins 中添加“if 条件”
    • 您不再需要两个回调。您可以将我的示例用于一个回调。如果您不想为管理员设置以下内容,只需在方法 return true if admin? 的第一行添加
    • 我在答案中更改了sql查询,请检查。如果错误仍然存​​在,请报告您使用什么数据库?
    • 我使用 ummm sqlite....我将方法编辑为我到目前为止的方式。我终于可以登录了。但是当用户登录时......他跟随自己(不是管理员)和邀请者。所以它的一半在那里大声笑
    • 老兄非常感谢你坚持和帮助我。我希望我能给你加倍的评价。太棒了,终于成功了!! :)
    【解决方案2】:

    好的,试试这个

    def follow_inviter                                
     inviter = Invitation.find_all_by_recipient_email(email)
     inviter.each do |invite|
     unless self.follow?invite.sender
      self.follow!(invite.sender)
     end
     end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多