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) [1;37mPRAGMA table_info(financial_transactions); [4;35mSQL (0.000000) [0;37mPRAGMA table_info(splits); [4;33mSQL (0.010000) [1;37mPRAGMA table_info(splits); [4;35mSQL (0.000000) [0;37mBEGIN [4;33mSQL (0.000000) [1;37mINSERT INTO splits (''account_id'', ''valuedate'', ''amount'', ''financial_transaction_id'', ''comment'') VALUES(NULL, NULL, NULL, NULL, NULL) [4;35mSQL (0.050000) [0;37mCOMMIT 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
Michel Rasschaert
2004-Dec-04 18:12 UTC
Re: Validation not working with << on collection ?
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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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) [1;37mPRAGMA > table_info(financial_transactions); > [4;35mSQL (0.000000) [0;37mPRAGMA table_info(splits); > [4;33mSQL (0.010000) [1;37mPRAGMA table_info(splits); > [4;35mSQL (0.000000) [0;37mBEGIN > [4;33mSQL (0.000000) [1;37mINSERT INTO splits (''account_id'', > ''valuedate'', ''amount'', ''financial_transaction_id'', ''comment'') > VALUES(NULL, NULL, NULL, NULL, NULL) > [4;35mSQL (0.050000) [0;37mCOMMIT > 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 >
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Michel Rasschaert wrote:> Here is my problem : Sometimes validation is ignoredWhat other times is validation ignored?> notably when using << on a has_many collection. see the example.See Ticket #185: http://dev.rubyonrails.org/trac.cgi/ticket/185 I think << should only associate records, not silently create new ones or save/validate content fields. has_many << updates the associate record''s foreign key and has_and_belongs_to_many << inserts a row in the join table. Neither touches the associate record''s content; hence, neither requires full save/validation. jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.6 (Darwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFBshvhAQHALep9HFYRAlNrAKC/kMQkVMykUdA1n+5LBjFoivPC1ACg3tK+ 1enl9ydqsumdssWVht+0sCo=my7b -----END PGP SIGNATURE-----