如何在 Rails 3 中实现 aasm 来实现我想要的功能?

发布于 2024-10-15 20:25:06 字数 1092 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼前雾蒙蒙 2024-10-22 20:25:06

经过思考,结果是这样的:

你没有在计划中制定状态是对的,不知道我在想什么。要么在 User 模型中执行此操作,要么创建一个 Account 模型,该模型belongs_to :user。然后,在您的帐户中尝试这些(这都是关于逻辑的,所以如果您想要更多状态,只需弥补):

aasm_initial_state :free

aasm_state :free
aasm_state :paid
aasm_state :disabled

aasm_event :pay do
  transitions :to => :paid, :from => [:free, :disabled]
end

aasm_event :disable do
  transitions :to => :disabled, :from => [:free,:paid]
end

因此,当您创建新用户时,也为其建立一个帐户。不用担心创建帐户时的初始状态,创建帐户时它会自动设置为“免费”。或者,如果听起来更容易,可以按照您的建议在 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 the User model, or make an Account model, which belongs_to :user. Then, in your account try these (it's all about logics, so if you want more states, just mak'em up):

aasm_initial_state :free

aasm_state :free
aasm_state :paid
aasm_state :disabled

aasm_event :pay do
  transitions :to => :paid, :from => [:free, :disabled]
end

aasm_event :disable do
  transitions :to => :disabled, :from => [:free,:paid]
end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文