2

私はRails 3 に書いてあるように、composed_of モデルと検証を使ってカスタム検証を追加しようとしましたが、奇妙なエラーが発生しました: "NoMethodError: undefined method `marked_for_destruction?' お金のために"

そして、何が悪いのかわかりません。

手伝って頂けますか?

モデル:

# == Schema Information
#
# Table name: transactions
#
#  id                  :integer          not null, primary key
#  text                :string(255)
#  amount_cents        :integer          default(0), not null
#  ...
class Transaction < ActiveRecord::Base
  attr_accessible :text, :amount, ...

  validates :text, :length => { :maximum => 255 }

  composed_of :amount, :class_name => "Money", :mapping => %w(amount_cents cents),
              :converter => Proc.new { |value| Money.to_money(value) }
  validates :amount, :presence => true, :numericality => { :greater_than_or_equal_to  => 0 }
  validates_associated :amount
  ...
end

class Money
  attr_reader :cents

  def initialize(cents)
    @cents = cents
  end

  class << self

    def to_money(str_money)
      cents = (str_money.to_f * 100).to_i
      Money.new(cents)
    end

    def to_money?(str_money)
      /\A\d+(\.\d+)?\z/ == str_money.to_s
    end

  end

  def ==(value)
    @cents == self.class.to_money(value).cents
  end

  def to_i
    @cents
  end

  def to_f
    @cents.to_f
  end

  def to_s
    return nil if @cents.nil?

    unit, subunit = @cents.abs.divmod(100)

    unit_str       = ""
    subunit_str    = ""
    fraction_str   = ""

    unit_str, subunit_str = unit.to_s, subunit.to_s

    subunit_str.insert(0, '0') while subunit_str.length < 2

    absolute_str =  "#{unit_str}.#{subunit_str}#{fraction_str}"

    absolute_str.tap do |str|
      str.insert(0, "-") if @cents < 0
    end
  end

  def inspect
    "#<Money cents:#{@cents} to_s:#{self.to_s}>"
  end  
end

エラー:

1.9.3p194 :001 > t = Transaction.new
 => #<Transaction id: nil, text: nil, amount_cents: 0, date: nil, created_at: nil, updated_at: nil, transaction_type_id: nil, account_id: nil, user_id: nil, trans_account_id: nil, trans_amount_cents: 0> 
1.9.3p194 :002 > t.amount = 100
 => 100 
1.9.3p194 :003 > t.valid?
NoMethodError: undefined method `marked_for_destruction?' for #<Money cents:10000 to_s:100.00>
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/validations/associated.rb:5:in `block in validate_each'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/validations/associated.rb:5:in `reject'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/validations/associated.rb:5:in `validate_each'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validator.rb:153:in `block in validate'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validator.rb:150:in `each'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validator.rb:150:in `validate'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:310:in `_callback_before_21'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:462:in `_run__74709952__validate__911291598__callbacks'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validations.rb:227:in `run_validations!'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:403:in `_run__74709952__validation__911291598__callbacks'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validations.rb:194:in `valid?'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/validations.rb:69:in `valid?'
    from (irb):3
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
    from /home/alexvs/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
4

0 に答える 0