0

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

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

在邀请中,受邀者的电子邮件保存为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
4

1 回答 1

1

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

  1. accepted在邀请中添加字段

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

你得到你想要的@user.sent_invitations.accepted.count

2. 执行以下查询

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

于 2013-01-31T22:07:51.747 回答