[Rails] Validation not working with << on collection ?

Michel Rasschaert michel.rasschaert at gmail.com
Sat Dec 4 18:07:52 GMT 2004


Hi,

Here is my problem : Sometimes validation is ignored, notably when
using << on a has_many collection. see the example.

# split.rb
class Split < ActiveRecord::Base
    # Associations
    belongs_to :account
    belongs_to :financial_transaction
    
    protected
        def validate
            errors.add_on_base('dumb error')
        end
end


# transaction_controller.rb
def test
    f = FinancialTransaction.new
    f.splits << Split.new
    render_text('See log for inserts')    
end

Result in production.log

Processing TransactionController#test (for 127.0.0.1 at Sat Dec 04
19:03:02 Paris, Madrid 2004)
  Parameters: {"action"=>"test", "id"=>nil, "controller"=>"transaction"}
  SQL (0.000000)   PRAGMA
table_info(financial_transactions);
  SQL (0.000000)   PRAGMA table_info(splits);
  SQL (0.010000)   PRAGMA table_info(splits);
  SQL (0.000000)   BEGIN
  SQL (0.000000)   INSERT INTO splits ('account_id',
'valuedate', 'amount', 'financial_transaction_id', 'comment')
VALUES(NULL, NULL, NULL, NULL, NULL)
  SQL (0.050000)   COMMIT
Completed in 0.200000 (4 reqs/sec) | DB: 0.060000 (30%)


Why is the split saved ? I think it shouldn't because it's not valid isn't it ?

Thanks


More information about the Rails mailing list