2010-06-07 42 views
1

嘿,我想知道是否有人可以幫我解決問題,基本上id在收到PayPal通知後會有Account.create,有一個對應的簡單購物車模型在購物車中line_items所以add_account_to_market看起來像這樣的僞代碼after_create:在DB中創建一個新行

def add_account_to_market 
    if status == "Completed" 
     find the line items(via cart_id) that correspond to the cart.id that just been paid 
     create an account with user_id set to the current carts user id 
    end 
end 

我從來沒有試圖做這樣的事情在Rails和它不工作,我一直拉我的頭髮了一夜試圖解決這一問題,希望有人可以幫助或指引我正確的方向。謝謝:)

class PaymentNotification < ActiveRecord::Base 
    belongs_to :cart 
    serialize :params 
    after_create :mark_cart_as_purchased 
    after_create :add_account_to_market 

    private 

    def mark_cart_as_purchased 
    if status == "Completed" 
     cart.update_attribute(:purchased_at, Time.now) 
     cart.update_attribute(:paid, true) 
    end 
    end 

    def add_account_to_market 
    if status == "Completed" 
     l = LineItem.find(:all, :conditions => "cart_id = '#{cart.id}'") 
     for l.quantity 
     Account.new(:user_id => cart.user_id) 
     end 
    end 
    end 
end 

PS mark_cart_as_purchased方法工作正常,有問題,其只是add_account_to_market IM。

回答

0

我覺得應該是:

Account.create(:user_id => cart.user_id) 

而且for l.quantity部分不應該存在。

1

add_account_to_market中創建新帳戶後,您不會致電save,因此在該方法返回後它們不會保留。

0

我結束了這個

def add_account_to_market 
    if status == "Completed" 
     l = LineItem.find(:first, :conditions => "cart_id = '#{cart.id}'") 
     l.quantity.times do 
     Account.create(:user_id => cart.user_id) 
    end 
end 

非常感謝您的回答。下次我看到這樣的問題時,我覺得不適合睡覺。 :P