0

だから私はこのBalanced Payments tutorialに従っていて、私のアプリケーションに合うように支払いモデルを微調整しようとしました.

オリジナル:

class Payment

  def initialize(email, amount, credit_card_hash)
    @email = email
    @amount = (amount * 100).to_i
    @credit_card_hash = credit_card_hash
    @buyer = nil
    @card = nil
    @errors = []
  end

end

私の簡易版(フルモデルのコードで更新):

require 'balanced'

class Transaction < ActiveRecord::Base

  attr_reader :errors, :amount

  def initialize(amount)
    @amount = (amount * 100).to_i
    @buyer = nil
    @card = nil
    @errors = []
  end

    def charge
      begin
        find_or_create_buyer
        debit_buyer
        credit_owner
        return true
      rescue
        return false
      end
    end

  private    

  def find_or_create_buyer
      begin
        @buyer = current_user.balanced_customer
      rescue
        @errors << 'Your account is invalid'
      end
  end

  def debit_buyer
    begin
      payment = @buyer.debit(@amount, "Test transaction")
    rescue
      @errors << 'Your credit card could not be charged'
    end
  end

  def credit_owner
    begin
      Balanced::Marketplace.my_marketplace.owner_account.credit(amount)
    rescue
      @errors << 'Your credit card payment did not go through.'
    end
  end


end

問題は、Rails コンソールからクラスをインスタンス化しようとするたびに、純粋な Ruby エラーのように見えることです。

> payment = Transaction.new(0.01)
 output error: #<NoMethodError: undefined method `has_key?' for nil:NilClass>

私はグーグルで検索しましたが、良い答えを思いつくことができませんでした。

何か案は?

4

2 に答える 2

2

アクティブなレコードでは、初期化は属性のハッシュで行われ、初期化子をオーバーライドしています

http://blog.hasmanythrough.com/2007/1/22/using-faux-accessors-to-initialize-values

于 2013-12-01T22:01:28.667 に答える
1

まあ、これは私が何をしたとしても、エラーを止めているようです。

これが後で問題を引き起こすかどうかはわかりません。

  def initialize(args = {})
    super
    @my_cache = {}
    @amount = args[:amount]
    @buyer = nil
    @card = nil
    @errors = []
  end
于 2013-10-12T00:36:14.993 に答える