嘿伙计们,我想知道是否有人可以帮助我解决我遇到的问题,基本上就像收到PayPal通知后会有Account.create一样,有一个简单的购物车模型对应购物车内的line_items所以add_account_to_market会在伪代码中看起来像这样
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 :(得分:1)
在save
创建新帐户后,您不会在新帐户上调用add_account_to_market
,因此在该方法返回后,它们不会保留。
答案 1 :(得分:0)
我认为应该是:
Account.create(:user_id => cart.user_id)
for l.quantity
部分不应该在那里。
答案 2 :(得分: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