Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails NoMethodError on .save

I ran the code @transaction = Transaction.new Then I gave it some values:

<Transaction id: nil, debit_uri: "d8hmFJ89CIQUZMBoiPMnvWkQJW/bank_...", credit_uri: "d8hmciqLOg9bCIQUZMBoiPMnvWkQJW/cards...", seller_id: 2, buyer_id: 6, product_id: 31, price: #<BigDecimal:b4a6115c,'0.45E2',9(36)>, ship_price: #<BigDecimal:b4a61094,'0.123E3',9(36)>, ship_method: "fedex", created_at: nil, updated_at: nil>

but when I do @transaction.save! bang(!) or not I get the error:

NoMethodError: undefined method `clear' for nil:NilClass
from /home/alain/.rvm/gems/ruby-1.9.3-head/gems/activemodel-3.2.13/lib/active_model/validations.rb:194:in `valid?'

so I don't know where to look for the error being how my model has little to nothing and there is no method called clear.

class Transaction < ActiveRecord::Base
  attr_accessible :buyer_id, :credit_uri, :debit_uri, :price, :product_id, :seller_id, :ship_method, :ship_price
  require 'balanced'  
  attr_reader :errors
end
like image 643
Alain Goldman Avatar asked Dec 05 '22 09:12

Alain Goldman


1 Answers

Regarding to Rails codebase, the error comes from:

attr_reader :errors

Look here. Try to remove it from your model.

Why?

Since you override the errors attributes and did not set it when creating your transaction instance, Rails is trying to do:

nil.clear
like image 80
Pierre-Louis Gottfrois Avatar answered Dec 07 '22 00:12

Pierre-Louis Gottfrois