2011-02-04 86 views
2

我是Rails n00b,並且已被告知,爲了讓我跟蹤用戶帳戶的狀態(即付費,未付費(因此禁用),免費試用等),我應該使用'AASM'寶石。我如何在Rails 3中實現aasm以實現我想要的功能?

因此,我發現了一個似乎是最受歡迎的:https://github.com/rubyist/aasm但是說明非常模糊。

我有一個用戶模型和一個計劃模型。用戶的模型管理你可能期望的一切(用戶名,密碼,名字等)。計劃模型管理用戶應該分配給的訂閱計劃(帶有限制)。

所以我想弄清楚如何使用AASM創業板做我想做的事,但不知道從哪裏開始。

我是否創建新模型?那麼我是否需要在我的用戶模型和AASM模型之間建立關係?我如何建立關係?如在用戶'has_many'狀態?這對我來說似乎沒有多大意義。

任何指導將非常感激。

謝謝。

編輯:如果別人是像我這樣的AASMs混淆,這裏是它們的功能在Rails的一個很好的解釋以優良的鄉親羨慕實驗室:http://blog.envylabs.com/2009/08/the-rails-state-machine/

EDIT2:這個怎麼樣:

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 
+0

envylabs鏈接不起作用了。以下是該博文的工作鏈接:http://madewithenvy.com/ecosystem/articles/2009/rails-state-machine/ – 2015-10-20 19:50:54

回答

3

給出它認爲,這是什麼就出來了:

你是正確的Plan不做狀態,不知道我在想什麼。要麼在User模型中做,要麼做一個Account模型,其中belongs_to :user。然後,在你的帳戶請嘗試以下(這是所有關於性的邏輯,所以如果你想要更多的狀態,只是mak'em起來):

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型號中。

+0

這肯定有助於爲我提供更多的東西 - 讓我一直在閱讀。非常感謝這個解釋。 – marcamillion 2011-02-04 21:57:13