为什么这个对象不会保存?

时间:2013-10-08 23:24:17

标签: ruby-on-rails ruby debugging pry

我有以下费用.rb:

class Charge < ActiveRecord::Base

  serialize :response, Hash

  include HTTParty
  belongs_to :order  

  def charge_url
    return "#{Pin.base_url}/1/charges"
  end

  def submit(ip)
    @authorization = {:username => Pin.secret_key, :password => Pin.public_key}
    @params =  {
      'currency' => "USD",
      'description' => "One Testivate Retail Site Search Benchmark report",
      'amount' => 99900,
      'ip_address' => (ip),
      'email' => self.order.user.email,
      'card' => {'number' => self.number,
                'expiry_month' => self.expiry_month,
                'expiry_year' => self.expiry_year,
                'cvc' => self.cvc,
                'name' => self.name,
                'address_line1' => self.address_line1,
                'address_line2' => self.address_line2,
                'address_city' => self.address_city,
                'address_postcode' => self.address_postcode,
                'address_state' => self.state,
                'address_country' => self.country }}
    response = HTTParty.post(self.charge_url, :query => @params, :basic_auth => @authorization)
    self.update_attributes :token => response["response"]["token"], :display_number => response["response"]["display_number"], :response => response
  end    

end

当我在控制台中调用self方法时,我得到了这个:

[30] pry(#<Charge>)> self
+----+----------+---------+-----------+-----------+----------+----------+-----------+----------+----------+----------+----------+----------+-----+----------+----------+----------+----------+----------+-------+----------+----------+
| id | order_id | user_id | token_... | token     | displ... | creat... | update... | card_... | custo... | number   | expir... | expir... | cvc | name     | addre... | addre... | addre... | addre... | state | country  | response |
+----+----------+---------+-----------+-----------+----------+----------+-----------+----------+----------+----------+----------+----------+-----+----------+----------+----------+----------+----------+-------+----------+----------+
| 2  | 9        |         |           | ch_O5d... |          | 2013-... | 2013-1... |          |          | 55200... | 12       | 2014     | 123 | Rolan... | 42 Se... |          | Lathlain |          | 6454  | Austr... | {"res... |
+----+----------+---------+-----------+-----------+----------+----------+-----------+----------+----------+----------+----------+----------+-----+----------+----------+----------+----------+----------+-------+----------+----------+
1 row in set
[31] pry(#<Charge>)> self.save
NoMethodError: undefined method `name' for nil:NilClass
from /Users/steven/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.11/lib/active_record/transactions.rb:274:in `rescue in rollback_active_record_state!'
down
Error: At bottom of stack, cannot go further!
[33] pry(#<Charge>)> up

Test failure: 

Frame number: 1/97
Frame type: method

From: /Users/steven/.rvm/gems/ruby-1.9.2-p320/gems/pry-remote-0.1.7/lib/pry-remote.rb @ line 284 Object#remote_pry:

    283: def remote_pry(host = PryRemote::DefaultHost, port = PryRemote::DefaultPort)
 => 284:   PryRemote::Server.new(self, host, port).run
    285: end

如果我再次上去,我最终会进入提交方法。 charge.rb模型中没有明确定义回调。我怎么能找到不能保存的NilClass?

1 个答案:

答案 0 :(得分:0)

修正如下:

self.update_attributes :token => response["response"]["token"], :display_number => response["response"]["display_number"], :response => response.to_hash

(无论如何,回复都是一个哈希值,但添加.to_hash的工作原理很he,这很好。)