[Rails] Re: Validation not working with << on collection ?
Michel Rasschaert
michel.rasschaert at gmail.com
Sat Dec 4 18:12:22 GMT 2004
I should add :
# financial_transaction.rb
require 'split'
class FinancialTransaction < ActiveRecord::Base
# Associations
has_many :splits
end
Thanks for your attention :)
On Sat, 4 Dec 2004 19:07:52 +0100, Michel Rasschaert
<michel.rasschaert at gmail.com> wrote:
> 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"}
> [4;33mSQL (0.000000)[m [1;37mPRAGMA
> table_info(financial_transactions);[m
> [4;35mSQL (0.000000)[m [0;37mPRAGMA table_info(splits);[m
> [4;33mSQL (0.010000)[m [1;37mPRAGMA table_info(splits);[m
> [4;35mSQL (0.000000)[m [0;37mBEGIN[m
> [4;33mSQL (0.000000)[m [1;37mINSERT INTO splits ('account_id',
> 'valuedate', 'amount', 'financial_transaction_id', 'comment')
> VALUES(NULL, NULL, NULL, NULL, NULL)[m
> [4;35mSQL (0.050000)[m [0;37mCOMMIT[m
> 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