# Doing error checking before saving my object to db?

**URL:** <https://www.ruby-forum.com/t/doing-error-checking-before-saving-my-object-to-db/129043>\
**Category:** Rails\
**Created:** [February 13, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/doing-error-checking-before-saving-my-object-to-db/129043 "2008-02-13T19:21:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 13, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/doing-error-checking-before-saving-my-object-to-db/129043/1 "2008-02-13T19:21:24Z")

</div>

Hi,

I have two models – order and line\_item (with attributes item\_number  
and description) where an order has many line\_items. In my order  
controller, I have this action after a user has submitted his order:

```
    def summary
            @ec_order = EcOrder.new(params[:ec_order])
            # do some error checking
            session[:ec_order] = @ec_order
    end

```

Prior to saving the submitted order to the session, I would like to do  
some error checking to make sure none of the line\_item.item\_number  
properties are empty or non-numeric. I already have this in my model:

class LineItem \< ActiveRecord::Base  
belongs\_to :order

```
    validates_presence_of :item_number
    validates_numericality_of :item_number, :integer_only => true

```

end

How can I activate this error checking for the line\_item object or is  
it only activated when I try and save the whole thing to the database?

Thanks, - Dave

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 13, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/doing-error-checking-before-saving-my-object-to-db/129043/2 "2008-02-13T20:08:57Z")

</div>

you can do:

if @ec.order.valid?  
session[:ec\_order] = @ec\_order  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 13, 2008, 8:46pm UTC](https://www.ruby-forum.com/t/doing-error-checking-before-saving-my-object-to-db/129043/3 "2008-02-13T20:46:35Z")

</div>

Gold. Thanks, Phil.

A follow up, if it’s not valid, how can I get a specific error  
message? For example, if a user didn’t enter an item number, I’d  
figure an error message would get generated somewhere because of the  
“validates\_presence\_of :item\_number” in the line\_item model.

- Dave

On Feb 13, 1:08 pm, Phil T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 13, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/doing-error-checking-before-saving-my-object-to-db/129043/4 "2008-02-13T20:55:26Z")

</div>

Objects should not be stored in session. Only ids are stored.

[Http://www.rubyplus.org](Http://www.rubyplus.org)  
Free Ruby & Rails screencasts

On Feb 13, 2008, at 11:45 AM, “removed\_email\_address@domain.invalid”  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 13, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/doing-error-checking-before-saving-my-object-to-db/129043/5 "2008-02-13T21:50:00Z")

</div>

um I think you need to look at “error\_messages\_for”  
[http://api.rubyonrails.org/classes/ActionView/Helpers/ActiveRecordHelper.html#M001005](http://api.rubyonrails.org/classes/ActionView/Helpers/ActiveRecordHelper.html#M001005)

have a google around and you’ll see how it works.

also: i dunno what BCP is trying to say but he’s probably right, I  
wouldn’t know about how to do things properly. I’m only good at getting  
things to work at all 😃

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 14, 2008, 12:58am UTC](https://www.ruby-forum.com/t/doing-error-checking-before-saving-my-object-to-db/129043/6 "2008-02-14T00:58:49Z")

</div>

Phil, I’ve read over the link. Thanks for sending it. My question is  
I don’t know what to put as the params value for error\_messages\_for.  
I tried “error\_messages\_for(@ec\_order)” but that produced an error.  
Here are my methods. I start on “new” and submit to “summary”, which  
should kick it back to “new” if there were errors …

```
    def new
            @user = User.find(session[:user_id])
            if (session[:ec_order] != nil)
                    @ec_order = session[:ec_order]
            else
                    @ec_order = EcOrder.new
                    1.times { @ec_order.ec_line_items.build }
            end
    end

    def summary
            @ec_order = EcOrder.new(params[:ec_order])
            if (!@ec_order.valid?)
                    flash[:notice] = "Order is invalid."
                    redirect_to :action => 'new'
            else
                    session[:ec_order] = @ec_order
            end
    end

```

BCP, I’m not storing ids because I haven’t stored my object to the  
database yet. I want to give the user a screen to confirm before the  
data gets stored. So that’s why I’m storing to a session. But if  
there’s a better way, please let me know.

Thanks to all, - Dave

BCP, But I haven’t stored anything to the database yet, so I don’t  
have an id.

On Feb 13, 2:50 pm, Phil T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 14, 2008, 9:15am UTC](https://www.ruby-forum.com/t/doing-error-checking-before-saving-my-object-to-db/129043/7 "2008-02-14T09:15:54Z")

</div>

well the way i’m using it in my current project (my first in RoR :D) is  
that in a view somewhere i have a form like this:

\<%form\_for :user do |form|%\>

some code

\<%=error\_messages\_for “user” %\>

some more code

\<% end %\>

so when you submit, if there are any errors on the submitted object, the  
controller returns you to this view, populates the error\_messages\_for  
and displays them to screen.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 14, 2008, 1:03pm UTC](https://www.ruby-forum.com/t/doing-error-checking-before-saving-my-object-to-db/129043/8 "2008-02-14T13:03:58Z")

</div>

validates\_associated also works.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 14, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/doing-error-checking-before-saving-my-object-to-db/129043/9 "2008-02-14T12:16:50Z")

</div>

ok i did some reading and it turns out that the session variable should  
only be used to store the user\_id for a logged in user
