after_create :create a new line in DB

Posted by Karl Entwistle on Stack Overflow See other posts from Stack Overflow or by Karl Entwistle
Published on 2010-06-07T02:44:55Z Indexed on 2010/06/07 2:52 UTC
Read the original article Hit count: 272

Filed under:
|

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"
      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.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby