2012-05-05 3 views
0

私はモデルPaymentを使用して、クレジットカード取引を追跡します。今、私はPayPalが成功したクレジットカード取引で返すいくつかの価値をマップするようにしています。これはnotify = ActiveMerchant::Billing::Integrations::Paypal::Notification.new(raw_post)(長さが長い)Rails - モデルはメソッドを通じて属性を保存しませんが、手作業で割り当てる場合(コンソールで)

によって処理されますが、私が書いた方法はコンソールでは機能しません。しかし、私がそれらを直接割り当てた場合、それは機能します。

Railsのコンソール

notify = ActiveMerchant::Billing::Integrations::Paypal::Notification.new(raw_post) 
# Works 
payment = Payment.new  
payment.raw_response = notify.raw 
save! # Save is successful 

# Does not work 
payment = Payment.new 
payment.map_paypal_return(notify) # save! call in this method does not trigger errors. 

私のクラスを見てみてください。コンソール

class Payment < ActiveRecord::Base 
    belongs_to :order 


    def map_paypal_return(notify) 
    puts "in mapping" => Outputted 
    raw_response = notify.raw 
    payment_status = notify.status 

    order_identifier = notify.item_id # Map ID to ID as a safety check bah. 

    payer_email = notify.params["payer_email"] 
    receiver_account = notify.account 
    auth_mode = notify.params["payment_type"] 

    transaction_identifier = notify.invoice 
    currency = notify.currency 
    amount = notify.gross 
    puts self.attributes # Returns attributes without the above assignments 

    save! # No errors triggered =(
    end 
end 

メソッドの呼び出し

1.9.2p318 :032 > notify.invoice # This should have been mapped to transaction_identifier 
=> "2012-146" 

1.9.2p318 :031 > p.map_paypal_return(notify) 
in mapping 
{"id"=>nil, "order_id"=>nil, "order_identifier"=>nil, "card_holder_name"=>nil, "auth_mode"=>nil, "amount"=>nil, "amount_string"=>nil, "currency"=>nil, "merchant_reference"=>nil, "transaction_identifier"=>nil, "status"=>nil, "other_errors"=>nil, "received_date"=>nil, "deleted"=>nil, "created_at"=>nil, "updated_at"=>nil, "state"=>"checkout", "payment_type"=>nil, "raw_response"=>nil, "receiver_account"=>nil, "payer_email"=>nil} 
    SQL (47.4ms) INSERT INTO "payments" ("amount", "amount_string", "auth_mode", "card_holder_name", "created_at", "currency", "deleted", "merchant_reference", "order_id", "order_identifier", "other_errors", "payer_email", "payment_type", "raw_response", "received_date", "receiver_account", "state", "status", "transaction_identifier", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["amount", nil], ["amount_string", nil], ["auth_mode", nil], ["card_holder_name", nil], ["created_at", Sat, 05 May 2012 20:32:33 SGT +08:00], ["currency", nil], ["deleted", nil], ["merchant_reference", nil], ["order_id", nil], ["order_identifier", nil], ["other_errors", nil], ["payer_email", nil], ["payment_type", nil], ["raw_response", nil], ["received_date", nil], ["receiver_account", nil], ["state", "checkout"], ["status", nil], ["transaction_identifier", nil], ["updated_at", Sat, 05 May 2012 20:32:33 SGT +08:00]] 
=> true 

私はどこかのRuby/Railsのの愚かな根本的な誤解をした場合、私は知りません。本当に助けに感謝します。

答えて

3

は、私はあなたのmap_paypal_return方法

raw_response = notify.raw 

raw_responseは、オブジェクトの属性の一つであるにしてい気づきます。それぞれのクラスのメソッド内のActiveRecordが提供する属性を割り当てるには、メソッドの属性に他の割り当てについて

self.raw_response = notify.raw 

と同様に行う必要があります。

これはルビの制限です。属性メソッドはattribute=メソッド(例えばraw_response=)を定義することによって実装され、修飾子(obj.またはself.)がない場合、rubyはステートメントがローカル変数代入であるとみなします。

+0

私はいつも、インスタンスメソッドのときにselfキーワードは必要ないと思っていました。ああ、コードの最後の数週間! –

+0

http://www.themomorohoax.com/2009/05/21/when-to-use-self-in-ruby-rails-models –

+0

あなたは私の一日を保存しました、ありがとう!あとで余分なコメントを削除してください。愚かなネクサスone.argh。 –

関連する問題