2013-01-31 25 views
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 

回答

1

我能想到的兩種不同的方式:

  1. 添加accepted場邀請

    你可以添加一個布爾字段名爲accepted邀請,默認值將是錯誤的,你就當receipent接受邀請設置爲true。然後創建一個名爲accepted範圍僅返回接受邀請 scope :accepted, where(accepted: true)

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

2什麼。做以下查詢

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

+0

謝謝@Khaled!我用#2。很棒! –