嘿,我想知道是否有人可以幫我解決問題,基本上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。