Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9252995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:06:09+00:00 2026-06-18T11:06:09+00:00

I am counting many user generated actions and for the most part it’s easy,

  • 0

I am counting many user generated actions and for the most part it’s easy, but with regard to one, more complex query, I am having trouble.

I have an invitations model and a user model and I can easily count the number of invitations the user sent, but I want to count the number of new members that signed up based on the invitations the existing member sent out.

In invitations, the invitees email is saved as recipient_email
Then, I know I can check that against new members email some how, but am not clear on the syntax.

Any help will be greatly appreciated. More information below.

Invitation Model:

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

User Model:

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

Invitation and User Tables:

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:06:10+00:00Added an answer on June 18, 2026 at 11:06 am

    I could think of two different ways:

    1. Add accepted field in invitations

      You could add a boolean field for invitations named accepted, the default value will be false and you set it to true when the receipent accepts the invitation. Then you create a scope named accepted that returns only accepted invitations
      scope :accepted, where(accepted: true)

    You get what you want by @user.sent_invitations.accepted.count

    2 . Do the following query

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a quicker query of counting how many rows there are using a
I have 3 tables. users, campaigns, links. They have one-to-many relations. User have many
I'm trying to write a Counting sort function but for some reason it's getting
I'm creating a database that will store how many times a user downloads a
I've read and watched many tutorials on NSUserDefaults in the iPhone OS/iOS SDK, but
i have this code for counting how many digits where entered var tnnod=0; function
Is there any portable (Windows & Linux) way of counting how many milliseconds elapsed
I have a table: Hourly_Sales that is like this (counting how many buy something
I am using iRate Files in my Application But i am geting So many
I'm using C# in reading an XML file and counting how many elements there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.