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 8649775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:38:39+00:00 2026-06-12T13:38:39+00:00

I’m building a call-tracking application as a way to learn rails and twilio. Right

  • 0

I’m building a call-tracking application as a way to learn rails and twilio.

Right now, I have the model scheme plans has_many users has_many phones.

In the plans model, I have a parameter called max_phone_numbers.

What I’d like to do is to limit the number of phones a user has based on the max_phone_numbers the plan gives.

The flow looks something like this :

1) User buys a bunch of phone numbers
2)When User.phones.count = max_phone numbers, then ability to buy more phone numbers is disabled, and a link pops up to the upgrade_path

I’m not quite sure how I would go about doing this though. What are the combinations of things I would need to do in my model, and in my controller?

What would I define in my controller, in such a way that in the view I can warp if/then statements around the buttons?

i.e if limit is reached, than show this, else show button

What would I put in my models to prevent someone from just visiting the link instead?

Any guidance, or resources on doing something like this would be greatly appreciated

Here’s my current user model

# == Schema Information
#
# Table name: users
#
#  id                    :integer          not null, primary key
#  name                  :string(255)
#  email                 :string(255)
#  created_at            :datetime         not null
#  updated_at            :datetime         not null
#  password_digest       :string(255)
#  remember_token        :string(255)
#  twilio_account_sid    :string(255)
#  twilio_auth_token     :string(255)
#  plan_id               :integer
#  stripe_customer_token :string(255)
#

# Twilio authentication credentials

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, :plan_id, :stripe_card_token
  has_secure_password
  belongs_to :plan
  has_many :phones, dependent: :destroy

  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token

  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: true

  validates :password, presence: true, length: { minimum: 6 }, on: :create
  validates :password_confirmation, presence: true, on: :create
  validates_presence_of :plan_id

  attr_accessor :stripe_card_token

  def save_with_payment
    if valid?
      customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
      self.stripe_customer_token = customer.id
      save!
    end
  rescue Stripe::InvalidRequestError => e
    logger.error "Stripe error while creating customer: #{e.message}"
    errors.add :base, "There was a problem with your credit card."
    false
  end

    def create_twilio_subaccount     
      @client = Twilio::REST::Client.new(TWILIO_PARENT_ACCOUNT_SID, TWILIO_PARENT_ACCOUNT_TOKEN)
      @subaccount = @client.accounts.create({:FriendlyName => self[:email]})
      self.twilio_account_sid = @subaccount.sid
      self.twilio_auth_token  = @subaccount.auth_token
      save!
    end

  private

      def create_remember_token
        self.remember_token = SecureRandom.urlsafe_base64
      end


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-12T13:38:41+00:00Added an answer on June 12, 2026 at 1:38 pm

    You could add a custom validation to your Phone model to check if a user has reached their limit. That would prevent any new Phone’s from being created if the user has reached their limit.

    In your User class

    def at_max_phone_limit?
      self.phones.count >= self.plan.max_phone_numbers
    end
    

    In your Phone class

    validate :check_phone_limit, :on => :create
    
    def check_phone_limit
      if User.find(self.user_id).at_max_phone_limit?
        self.errors[:base] << "Cannot add any more phones"
      end
    end
    

    In your view/form, you would do something like this

    <% if @user.at_max_phone_limit? %>
      <%= link_to "Upgrade your Plan", upgrade_plan_path %>
    <% else %>
      # Render form/widget/control for adding a phone number
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.