Rails: how can I automatically update a user account after reaching a limit?

I have a rails app that stores files where a user can subscribe to three plans:

  • plan 1: free trial up to 50 files for 30 days
  • plan 2: up to 250 files
  • plan 3: up to 500 files

How can I automatically update / downgrade user plans when:

  • The 30-day trial is ending or the user downloads more than 50 files.
  • File limit exceeded and goes into different brackets
  • Or the file is deleted and the user lowers the level of

How do I configure my Rails application to "Watch" the user account for these changes?

Is there a better way than to stick to logic in the file controller, create and delete actions? How about a 30-day trial logic? Thank!

Note. I can handle the actual switching of the subscription just fine, just looking for the logic to monitor and run the switches.

+3


source to share


2 answers


Set the association callbacks in the user plan. Assuming you have a has_many relationship to Plan, in User.rb you can have something like

has_many :plans, :through => :user_plans,
  :after_add => :check_plan_eligibility,
  :after_remove => :check_plan_eligibility

      



and then

protected
def check_plan_eligibility(obj)
  # Do checks here based on your rules, and update the user plan ID accordingly
end

      

+2


source


Observers ( http://api.rubyonrails.org/classes/ActiveRecord/Observer.html )

or



ActiveRecord :: Callbacks ( http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html )

The 30-day trial version can be verified when the user logs in. The rest can be done using callbacks when the user is updated.

0


source







All Articles