Cameron,
Here''s one suggestion, though there may be an easier way. You
could add a mixin, or extend the validations module via a
module_eval, and alias the destroy method to your own that calls valid?.
I would suggest then also adding a custom validate_on_destroy
handler - see version 1.0.7 of the salted login generator for how I
do this. You may want to consider proposing an official
validate_on_destroy handler, because this sounds like it may be
generally useful enough that other people would be interested.
Joe
On May 8, 2005, at 2:53 PM, Cameron Price wrote:
> I''m building a little application with Rails, and I''m
becoming a huge
> fan of the validations in ActiveRecord. Recently, though I ran into a
> problem which to me feels like a job for Validations, but I can''t
see
> a way to use them.
>
> My model consits of a Trade object, which represents either buying or
> selling some security. I need to require that at no point has a user
> sold more than he''s bought (no short selling). This is
fantastically
> easy to accomplish when entering a new trade, and when modifying
> existing trades, I just add a simple validate in my Trade object.
> Unfortunately, it''s possible to get around the validation by
deleting
> a prior trade, and I can''t figure out the best way to deal with
this.
>
> My first thought was to override before_destroy, but it doesn''t
look
> like there''s a way to write code that will abort the destroy if
it''s
> not legal, in the same way that I can for create and update. I could
> handle this in the controllers, of course, but this is a very
> model-related problem, and letting the controllers worry about it
> feels like a hack to me.
>
> Do I need to override the destroy method itself, or am I missing
> something?
>
> Thanks in advance,
>
> Cameron
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>