基于邀请计数成员 - 查询

时间:2013-01-31 21:45:16

标签: ruby-on-rails

我正在计算许多用户生成的操作,并且在很大程度上它很容易,但是对于一个更复杂的查询,我遇到了麻烦。

我有一个邀请模型和一个用户模型,我可以很容易地计算用户发送的邀请数量,但是我想根据现有成员发出的邀请来计算注册的新成员数量

在邀请中,被邀请者电子邮件将保存为recipient_email 然后,我知道我可以针对新成员email检查一些如何,但我不清楚语法。

任何帮助将不胜感激。更多信息如下。

邀请模特:

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'

  validates_presence_of :recipient_email
  validates_uniqueness_of :recipient_email, :message => '%{value} has already been invited'
  validate :recipient_is_not_registered
  validate :sender_has_invitations, :if => :sender

  default_scope order: 'invitations.created_at DESC'

  before_create :generate_token
  before_create :decrement_sender_count, :if => :sender

  after_create do |invitation|
   InvitationMailer.delay.invitation_email(self)
  end

  def invitee
    User.find_by_email(self.recipient_email)
  end

  def invitee_registered?
    !invitee.blank?
  end

  def invitee_submitted?
      !invitee.try(:submissions).blank?
  end

  private
  def recipient_is_not_registered
    errors.add :recipient_email, 'is already registered' if User.find_by_email(recipient_email)
  end

  def sender_has_invitations
    unless sender.invitation_limit > 0
      errors.add_to_base "You have reached your limit of invitations to send. 
                      You can contact Lumeo if you'd like to request more."
    end
  end

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

  def decrement_sender_count
    sender.decrement! :invitation_limit
  end
end

用户模型:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :mailchimp


  attr_accessible :name, :email, :password, :password_confirmation, 
                  :remember_me, :role_id, :role_ids, :image_attributes,
                  :terms, :profile_attributes, :current, :image, :roles,
                  :invitation_token, :join_mailing_list, :affiliate_id,
                  :invitation_affiliate_token, :affiliation, :referrer_id
  validates_uniqueness_of :email
  VALID_NAME_REGEX = /[\w]+([\s]+[\w]+){1}+/
  validates :name, presence: true, 
                    format: {with: VALID_NAME_REGEX} 

  #invitation
  has_many :sent_invitations, :class_name => 'Invitation', :foreign_key => 'sender_id'
  belongs_to :invitation

  def invitation_token
    invitation.token if invitation
  end
  def invitation_token=(token)
    self.invitation = Invitation.find_by_token(token)
  end
  before_create :set_invitation_limit
  has_one :invitation_affiliate, :class_name => "Affiliate", :foreign_key => 'token', :primary_key => 'invitation_affiliate_token'

  private

  def set_invitation_limit
    self.invitation_limit = 100
  end
end

邀请和用户表:

create_table "users", :force => true do |t|
  t.string   "email",                      :default => "",    :null => false
  t.string   "encrypted_password",         :default => "",    :null => false
  t.string   "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",              :default => 0
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string   "current_sign_in_ip"
  t.string   "last_sign_in_ip"
  t.datetime "created_at",                                    :null => false
  t.datetime "updated_at",                                    :null => false
  t.integer  "role_id"
  t.string   "name"
  t.integer  "invitation_id"
  t.integer  "invitation_limit"
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

1 个答案:

答案 0 :(得分:1)

我可以想到两种不同的方式:

  1. 在邀请中添加accepted字段

    您可以为名为accepted的邀请添加布尔字段,默认值为false,并且当receipent接受邀请时将其设置为true。然后,您创建一个名为accepted的范围,该范围仅返回已接受的邀请 scope :accepted, where(accepted: true)

  2. @user.sent_invitations.accepted.count

    可以得到你想要的东西

    2。执行以下查询

    User.where(email: @user.sent_invitations.map(&:recipient_email)).count