# Rails validations app specific problems

**URL:** <https://www.ruby-forum.com/t/rails-validations-app-specific-problems/186944>\
**Category:** Rails\
**Created:** [April 14, 2010, 5:47pm UTC](https://www.ruby-forum.com/t/rails-validations-app-specific-problems/186944 "2010-04-14T17:47:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [April 14, 2010, 5:47pm UTC](https://www.ruby-forum.com/t/rails-validations-app-specific-problems/186944/1 "2010-04-14T17:47:23Z")

</div>

Hi all,

I have a unique workflow where it’s making the use of validations  
difficult.

My app is for internal use in my company and I have a funky  
implementation (inspired by the e-commerce Railscasts episodes) of a  
cart. I have products which when I “add to cart” creates an order with a  
state of “incomplete”. The product is a line item of my order. Hope that  
makes sense.

Even though this is a little complicated, it works well (even though it  
may not be the best implementation - suggestions are welcome though!)

Now the problem is when I go to checkout and complete my order I want to  
validate certain things, like if my customer’s order number is present  
(validates\_presence\_of). This should be easy, but since the order has  
ALREADY been created I need to validate the presence of an attribute.

Also, I can’t just do this:

```auto
validates_presence_of :customer_order_number, :on => :update
```

Because it generates a validation error on my ‘/cart’ page which is  
where I can go and edit the product quantities for the client order.

**I’m sure the solution lies in validates\_presence\_of with the :if  
condition, but I just can’t figure out how to make it work.**

Below is my related app code.

Orders Controller:

```auto
  def update
    @order = Orders.find(params[:id])

    respond_to do |format|
        if @order.update_attributes(params[:order])
          flash[:notice] = 'Your cart has been updated.'
          format.html { redirect_to( current_cart_url ) }
          format.xml { head :ok }
        else
          format.html { redirect_to( current_cart_url ) }
          format.xml { render :xml => @order.errors, :status =>
:unprocessable_entity }
        end
    end

  end

  def cart
    create_current_order unless current_order
    @order = current_order
  end

  def checkout
    @order = current_order
  end

  def complete_order
    @order = current_order

    respond_to do |format|
      if @order.update_attributes(params[:order])
        @order.complete #sets order completed at date and state to
'complete'
        flash[:notice] = 'Thank you! Your order is being processed.'
        format.html { redirect_to( products_path ) }
        format.xml { head :ok }
      else
        format.html { render :action => 'checkout' }
        format.xml { render :xml => @order.errors, :status =>
:unprocessable_entity }
      end
    end

  end
```

I know this is kind of tough to understand, please let me know if you  
need more specifics or code. Thank you!

-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [April 14, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/rails-validations-app-specific-problems/186944/2 "2010-04-14T18:00:18Z")

</div>

On Apr 14, 4:47 pm, Tony T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```auto
> validates_presence_of :customer_order_number, :on => :update
> ```
> 
> Because it generates a validation error on my ‘/cart’ page which is  
> where I can go and edit the product quantities for the client order.
> 
> **I’m sure the solution lies in validates\_presence\_of with the :if  
> condition, but I just can’t figure out how to make it work.**

The :if option is just the name of an instance method that returns  
whether the validation should run (eg if the status has some value or  
something like that)

Fred
