I have a rails app which stores files, in which a user can subscribe to three plans:
我有一个存储文件的rails应用,用户可以订阅三个计划:
- plan 1: free trial up to 50 files for 30 days
- 计划一:免费试用50个文件,30天
- plan 2: up to 250 files
- 计划2:多达250个文件。
- plan 3: up to 500 files
- 计划3:最多500个文件
How would I go about automatically upgrading/downgrading the user plans when:
当:
- The 30 day trial ends or user uploads more than 50 files
- 30天的试用结束或用户上传超过50个文件
- File limit is exceeded and goes into another bracket
- 文件限制被超过并进入另一个括号
- Or a file is deleted and the user goes down a level
- 或者一个文件被删除,用户进入一个级别
How do I set the Rails app to "Watch" a user account for these changes?
如何设置Rails应用程序来“监视”用户帐户的这些更改?
Is there a better way than sticking logic in the Files controller create and delete actions? And what about the 30 day trial logic? Thank you!
有比在文件控制器中插入逻辑创建和删除操作更好的方法吗?那么30天的试验逻辑呢?谢谢你!
Note: I can handle the actual switching of the subscriptions just fine, just looking for logic to monitor and trigger the switches.
注意:我可以很好地处理订阅的实际切换,只是寻找逻辑来监视和触发开关。
2 个解决方案
#1
2
Setup association callbacks on the user's Plan. Assuming you have a has_many relationship to Plan, in User.rb you could have something like
设置关联回调用户的计划。假设您在User中有一个要计划的has_many关系。rb可以是这样的
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's plan ID accordingly
end
#2
0
Observers (http://api.rubyonrails.org/classes/ActiveRecord/Observer.html)
观察者(http://api.rubyonrails.org/classes/ActiveRecord/Observer.html)
or
或
ActiveRecord::Callbacks (http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)
ActiveRecord::回调(http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)
30 day trial could be checked upon user login. The rest could be done using callbacks when the user updates.
30天的试用可以在用户登录后进行检查。其余的可以在用户更新时使用回调来完成。
#1
2
Setup association callbacks on the user's Plan. Assuming you have a has_many relationship to Plan, in User.rb you could have something like
设置关联回调用户的计划。假设您在User中有一个要计划的has_many关系。rb可以是这样的
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's plan ID accordingly
end
#2
0
Observers (http://api.rubyonrails.org/classes/ActiveRecord/Observer.html)
观察者(http://api.rubyonrails.org/classes/ActiveRecord/Observer.html)
or
或
ActiveRecord::Callbacks (http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)
ActiveRecord::回调(http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)
30 day trial could be checked upon user login. The rest could be done using callbacks when the user updates.
30天的试用可以在用户登录后进行检查。其余的可以在用户更新时使用回调来完成。