如何在 Rails 3 中实现 aasm 来实现我想要的功能?
我是 Rails n00b,有人建议我为了跟踪用户帐户的状态(即付费、未付费(因此被禁用)、免费试用等),我应该使用“AASM”gem。
所以我找到了一个似乎最受欢迎的: https://github.com/rubyist/aasm但指示相当模糊。
我有一个用户模型和一个计划模型。用户模型管理您可能期望的所有内容(用户名、密码、名字等)。计划模型管理应分配给用户的订阅计划(有限制)。
所以我试图弄清楚如何使用 AASM gem 来做我想做的事情,但不知道从哪里开始。
我要创建一个新模型吗?然后我是否在我的 User 模型和 AASM 模型之间建立关系?我如何建立关系?例如,用户“has_many”表示?这对我来说似乎没有多大意义。
任何指导将不胜感激。
谢谢。
编辑:如果其他人像我一样对 AASM 感到困惑,这里有 Envy Labs 的优秀人员对它们在 Rails 中的功能的一个很好的解释:http://blog.envylabs.com/2009/08/the-rails-state-machine/
编辑2:这看起来怎么样:
include AASM
aasm_column :current_state
aasm_state :paid
aasm_state :free_trial
aasm_state :disabled #this is for accounts that have exceed free trial and have not paid
#aasm_state :free_acct
aasm_event :pay do
transitions :to => :paid, :from => [:free_trial, :disabled]
transitions :to => :disabled, :from => [:free_trial, :paid]
end
I am a Rails n00b and have been advised that in order for me to keep track of the status of my user's accounts (i.e. paid, unpaid (and therefore disabled), free trial, etc.) I should use an 'AASM' gem.
So I found one that seems to be the most popular: https://github.com/rubyist/aasm But the instructions are pretty vague.
I have a Users model and a Plan model. User's model manages everything you might expect (username, password, first name, etc.). Plan model manages the subscription plan that users should be assigned to (with the restrictions).
So I am trying to figure out how to use the AASM gem to do what I want to do, but no clue where to start.
Do I create a new model ? Then do I setup a relationship between my User model and the model for AASM ? How do I setup a relationship? As in, a user 'has_many' states ? That doesn't seem to make much sense to me.
Any guidance would be really appreciated.
Thanks.
Edit: If anyone else is confused by AASMs like myself, here is a nice explanation of their function in Rails by the fine folks at Envy Labs: http://blog.envylabs.com/2009/08/the-rails-state-machine/
Edit2: How does this look:
include AASM
aasm_column :current_state
aasm_state :paid
aasm_state :free_trial
aasm_state :disabled #this is for accounts that have exceed free trial and have not paid
#aasm_state :free_acct
aasm_event :pay do
transitions :to => :paid, :from => [:free_trial, :disabled]
transitions :to => :disabled, :from => [:free_trial, :paid]
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过思考,结果是这样的:
你没有在
计划
中制定状态是对的,不知道我在想什么。要么在User
模型中执行此操作,要么创建一个Account
模型,该模型belongs_to :user
。然后,在您的帐户中尝试这些(这都是关于逻辑的,所以如果您想要更多状态,只需弥补):因此,当您创建新用户时,也为其建立一个帐户。不用担心创建帐户时的初始状态,创建帐户时它会自动设置为“免费”。或者,如果听起来更容易,可以按照您的建议在
User
模型中进行。given it thought and this is what came out:
you're right about not making the states in the
Plan
, dunno what I was thinking. Either do it in theUser
model, or make anAccount
model, whichbelongs_to :user
. Then, in your account try these (it's all about logics, so if you want more states, just mak'em up):So when you create a new user, build an account for it too. Don't worry about the initial state at account creation, it will automatically be set to "free" when an account is created. Or, if it sounds easier, in the
User
model, as you suggested.