Does anybody have a case where you are validating multiple activerecord objects in a single request, and you want all of them to be validated, and you want all of them stored in the database, but not if any of them fail at validation? I was going over possible ways to do this, and I thought it might be a nice feature to include in the framework. Maybe a block method to put your code in that throws an exception if there are any validation errors (after processing everything in the block), or that keeps a boolean "is_valid" locally, and that way the transaction block could be optional. I''d appreciate any input. -Jeff _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Tobias Luetke
2005-Jan-23 15:21 UTC
Re: rolling back transactions based on validation errors
Maybe something like this:
if @request.post? and @record1.valid? and @record2.valid?
Any.transaction do
@record1.save or raise "?!"
@record2.save or raise "?!"
end
end
On Fri, 21 Jan 2005 15:42:37 -0700, Jeffrey Moss
<jeff-61uStg5MtoFWk0Htik3J/w@public.gmane.org>
wrote:>
> Does anybody have a case where you are validating multiple activerecord
> objects in a single request, and you want all of them to be validated, and
> you want all of them stored in the database, but not if any of them fail at
> validation? I was going over possible ways to do this, and I thought it
> might be a nice feature to include in the framework. Maybe a block method
to
> put your code in that throws an exception if there are any validation
errors
> (after processing everything in the block), or that keeps a boolean
> "is_valid" locally, and that way the transaction block could be
optional.
>
> I''d appreciate any input.
>
> -Jeff
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>
--
Tobi
http://www.hieraki.org - Open source book authoring
http://blog.leetsoft.com - Technical weblog
Duane Johnson
2005-Jan-23 19:36 UTC
Re: rolling back transactions based on validation errors
What is the "Any" class? On Sun, 23 Jan 2005 10:21:46 -0500, Tobias Luetke <tobias.luetke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Maybe something like this: > > if @request.post? and @record1.valid? and @record2.valid? > Any.transaction do > @record1.save or raise "?!" > @record2.save or raise "?!" > end > end > > > On Fri, 21 Jan 2005 15:42:37 -0700, Jeffrey Moss <jeff-61uStg5MtoFWk0Htik3J/w@public.gmane.org> wrote: > > > > Does anybody have a case where you are validating multiple activerecord > > objects in a single request, and you want all of them to be validated, and > > you want all of them stored in the database, but not if any of them fail at > > validation? I was going over possible ways to do this, and I thought it > > might be a nice feature to include in the framework. Maybe a block method to > > put your code in that throws an exception if there are any validation errors > > (after processing everything in the block), or that keeps a boolean > > "is_valid" locally, and that way the transaction block could be optional. > > > > I''d appreciate any input. > > > > -Jeff > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > -- > Tobi > http://www.hieraki.org - Open source book authoring > http://blog.leetsoft.com - Technical weblog > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jeremy Kemper
2005-Jan-23 20:21 UTC
Re: rolling back transactions based on validation errors
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Duane Johnson wrote:> What is the "Any" class?It''s something like the "any" key on your keyboard ;) Every model class has a transaction class method that begins, possibly rolls back, and commits a transaction on its database connection. Pseudocode: def transaction @connection.begin_transaction result = yield @connection.commit_transaction return result rescue Exception @connection.rollback_transaction raise end Additionally, every model has a transaction instance method which calls self.class.transaction; thus, you may use transactions with "Any" model class or instance. jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.6 (Darwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFB9AdkAQHALep9HFYRAsYhAJ9jesWLn8+t85nMrvNAFZQh8i01/gCfda3l beTImgsXPfssTkIvzBodY/U=ycwb -----END PGP SIGNATURE-----
jeff-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org
2005-Jan-24 01:45 UTC
Re: rolling back transactions based on validation errors
This is essentially what I did, but this calls valid twice, once in the
conditional and then again when you save (correct me if I''m wrong, I
glanced through the libraries). In order to prevent validation being
called twice though, I just use the transaction to roll back if there is a
validation failure. Something like this:
if @request.post?
all_valid = TRUE;
Any.transaction do
all_valid = FALSE unless @record1.save
> Maybe something like this:
>
> if @request.post? and @record1.valid? and @record2.valid?
> Any.transaction do
> @record1.save or raise "?!"
> @record2.save or raise "?!"
> end
> end
>
>
> On Fri, 21 Jan 2005 15:42:37 -0700, Jeffrey Moss
<jeff-61uStg5MtoFWk0Htik3J/w@public.gmane.org>
> wrote:
>>
>> Does anybody have a case where you are validating multiple activerecord
>> objects in a single request, and you want all of them to be validated,
>> and
>> you want all of them stored in the database, but not if any of them
fail
>> at
>> validation? I was going over possible ways to do this, and I thought it
>> might be a nice feature to include in the framework. Maybe a block
>> method to
>> put your code in that throws an exception if there are any validation
>> errors
>> (after processing everything in the block), or that keeps a boolean
>> "is_valid" locally, and that way the transaction block could
be
>> optional.
>>
>> I''d appreciate any input.
>>
>> -Jeff
>> _______________________________________________
>> Rails mailing list
>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>>
>>
>
>
> --
> Tobi
> http://www.hieraki.org - Open source book authoring
> http://blog.leetsoft.com - Technical weblog
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
jeff-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org
2005-Jan-24 01:51 UTC
Re: rolling back transactions based on validation errors
This is essentially what I did, but this calls valid twice, once in the
conditional and then again when you save (correct me if I''m wrong, I
glanced through the libraries). In order to prevent validation being
called twice though, I just use the transaction to roll back if there is a
validation failure. Something like this:
if @request.post?
all_valid = TRUE;
Any.transaction do
all_valid = FALSE unless @record1.save
all_valid = FALSE unless @record2.save
raise "error" unless all_valid
end
end
but what would be really cool is if you could do somehting like this:
if @request.post?
Any.transaction(:validate_all => :yes) do
@record1.save
@record2.save
end
end
And that would raise the exception after everything in the block had been
processed and there were validation errors.
P.S. Please excuse my incomplete post.
-Jeff
> Maybe something like this:
>
> if @request.post? and @record1.valid? and @record2.valid?
> Any.transaction do
> @record1.save or raise "?!"
> @record2.save or raise "?!"
> end
> end
>
>
> On Fri, 21 Jan 2005 15:42:37 -0700, Jeffrey Moss
<jeff-61uStg5MtoFWk0Htik3J/w@public.gmane.org>
> wrote:
>>
>> Does anybody have a case where you are validating multiple activerecord
>> objects in a single request, and you want all of them to be validated,
>> and
>> you want all of them stored in the database, but not if any of them
fail
>> at
>> validation? I was going over possible ways to do this, and I thought it
>> might be a nice feature to include in the framework. Maybe a block
>> method to
>> put your code in that throws an exception if there are any validation
>> errors
>> (after processing everything in the block), or that keeps a boolean
>> "is_valid" locally, and that way the transaction block could
be
>> optional.
>>
>> I''d appreciate any input.
>>
>> -Jeff
>> _______________________________________________
>> Rails mailing list
>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>>
>>
>
>
> --
> Tobi
> http://www.hieraki.org - Open source book authoring
> http://blog.leetsoft.com - Technical weblog
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>