Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 with composed_of model and validation get error "NoMethodError: undefined method `marked_for_destruction?'"
    text
    copied!<p>I have tried to add custom validation as is written in <a href="https://stackoverflow.com/questions/6707636/rails-3-with-composed-of-model-and-validation">Rails 3 with composed_of model and validation</a> but i got strange error: "NoMethodError: undefined method `marked_for_destruction?' for Money"</p> <p>And I don't understand what is wrong.</p> <p>Can you help me?</p> <p>Model:</p> <pre><code># == Schema Information # # Table name: transactions # # id :integer not null, primary key # text :string(255) # amount_cents :integer default(0), not null # ... class Transaction &lt; ActiveRecord::Base attr_accessible :text, :amount, ... validates :text, :length =&gt; { :maximum =&gt; 255 } composed_of :amount, :class_name =&gt; "Money", :mapping =&gt; %w(amount_cents cents), :converter =&gt; Proc.new { |value| Money.to_money(value) } validates :amount, :presence =&gt; true, :numericality =&gt; { :greater_than_or_equal_to =&gt; 0 } validates_associated :amount ... end class Money attr_reader :cents def initialize(cents) @cents = cents end class &lt;&lt; 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 &lt; 2 absolute_str = "#{unit_str}.#{subunit_str}#{fraction_str}" absolute_str.tap do |str| str.insert(0, "-") if @cents &lt; 0 end end def inspect "#&lt;Money cents:#{@cents} to_s:#{self.to_s}&gt;" end end </code></pre> <p>Error:</p> <pre><code>1.9.3p194 :001 &gt; t = Transaction.new =&gt; #&lt;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&gt; 1.9.3p194 :002 &gt; t.amount = 100 =&gt; 100 1.9.3p194 :003 &gt; t.valid? NoMethodError: undefined method `marked_for_destruction?' for #&lt;Money cents:10000 to_s:100.00&gt; 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 `&lt;top (required)&gt;' from script/rails:6:in `require' </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload