开发者

after_create :create a new line in DB

开发者 https://www.devze.com 2023-01-02 17:52 出处:网络
Hey guys I was wondering if anyone could help me with an issue im having, basically id like to have Account.create after a PayPal notification is received, There is a simple cart model which correspon

Hey guys I was wondering if anyone could help me with an issue im having, basically id like to have Account.create after a PayPal notification is received, There is a simple cart model which corresponds to line_items within the cart so add_account_to_market would look like this in pseudo code

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

Ive never tried to do something like this in Rails and its not working, Ive been pulling my hair out all night trying to fix this, hopefully someone can help or point me in the right direction. Thanks :)

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"
开发者_如何学Python      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 method is working fine, its just the add_account_to_market im having issues with.


You are not calling save on the new accounts after they are created in add_account_to_market, so they are not going to persist after that method returns.


I think it should be:

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

And the for l.quantity part shouldn't be there.


I ended up with this

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

Thanks very much for your answers. Next time I see a problem like this, I think ill sleep on it.  :P

0

精彩评论

暂无评论...
验证码 换一张
取 消