Doing error checking before saving my object to db?


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 =[:ec_order])
            # do some error checking
            session[:ec_order] = @ec_order

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


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

you can do:

if @ec.order.valid?
session[:ec_order] = @ec_order

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. [email protected]

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

Free Ruby & Rails screencasts

On Feb 13, 2008, at 11:45 AM, “[email protected]
<[email protected]

um I think you need to look at “error_messages_for”

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 :smiley:

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]
                    @ec_order =
                    1.times { }

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

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. [email protected]

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.

validates_associated also works.

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