Forum: Ruby on Rails Ignoring Validations In An Admin Tool?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A7db9ec803b5895ae5f916a74e2db329?d=identicon&s=25 HH (Guest)
on 2006-04-01 02:25
(Received via mailing list)
I have the following validation on and 'Order' model:

validates_presence_of :order_lines, :message => 'missing one or more
products.', :on => :save

Ok, great. An order has to have products. Ok.

Problem is, there are some old orders that don't have lines (don't ask)
and
in the admin tool we need to be able to work on these orders.

Problem is that we can't save them since they technically fail
validation.
In the admin tool I want to be able to get around this.

I'm using two methods to update order status, depending on if we're
doing
lots of 'em at a time:

Order.update(params[:item].keys, params[:item].values)

Or just one:

    @order = Order.find(params[:id])
    @order.update_attributes(params[:order])

So, can anyone offer any advice in this scenario to bypass validations
and
update the objects/save them?

Thx,
Hunter
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-01 02:52
(Received via mailing list)
On Fri, 2006-03-31 at 16:23 -0800, HH wrote:
> Problem is that we can't save them since they technically fail validation.
>     @order.update_attributes(params[:order])
>
> So, can anyone offer any advice in this scenario to bypass validations and
> update the objects/save them?
>
----
maybe I am dense but couldn't you just use sql to add what you need - be
they columns or rows ? ...i.e. insert into ... where ...

Craig
Cf7e5e4b5ca573eec214191fac420a2f?d=identicon&s=25 Dave Silvester (Guest)
on 2006-04-01 03:14
(Received via mailing list)
On Saturday 01 Apr 2006 01:23, HH wrote:
>     @order = Order.find(params[:id])
>     @order.update_attributes(params[:order])
>
> So, can anyone offer any advice in this scenario to bypass validations and
> update the objects/save them?

Kinda sounds to me like you want:
update_attribute_with_validation_skipping

See:
http://api.rubyonrails.com/classes/ActiveRecord/Va...

Though as far as I know, there's no way of doing update_attributes and
bypassing all validations.

Another possibility for you would be to put validation conditions into
your
model using :if =>

validates_presence_of :order_lines, :message => 'missing one or more
products.', :on => :save, :if => some_condition

HOWEVER, as far as I know, the models aren't supposed to be tightly
linked to
the controllers and have a good degree of enforced separation between
them,
so if you were going to check for the presence of a session variable (or
something) in your model, I think you'll be out of luck as that kind of
stuff
belongs in the controller.

There's probably a way you could do it but I can't think right now,
sorry!

~Dave

--

Dave Silvester
Rent-A-Monkey Website Development
http://www.rentamonkey.com/

PGP Key: http://www.rentamonkey.com/pgpkey.asc
2c51fec8183a5d21c4e11b430beabb47?d=identicon&s=25 Patrick Hurley (Guest)
on 2006-04-01 05:39
(Received via mailing list)
Another choice is to create the base model without the validations and
then inherit from it to add the validations. In the normal case use
the properly validated model, but when necessary fall back to the base
model.

pth
This topic is locked and can not be replied to.