# How do I tell an object it is invalid?

**URL:** <https://www.ruby-forum.com/t/how-do-i-tell-an-object-it-is-invalid/145750>\
**Category:** Rails\
**Created:** [August 13, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/how-do-i-tell-an-object-it-is-invalid/145750 "2008-08-13T17:46:47Z")\
**Posts on this page:** 3\
**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:** [August 13, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/how-do-i-tell-an-object-it-is-invalid/145750/1 "2008-08-13T17:46:47Z")

</div>

Hi,

How do I tell an object that it is not valid? My model is

class EcOrder \< ActiveRecord::Base  
has\_many :ec\_line\_items  
validates\_associated :ec\_line\_items  
…  
end

and in addition to the above, I want the model to only be valid is it  
has one or more valid :ec\_line\_items. So when creating it, can I set  
its status to invalid if I don’t detect it has been assigned multiple  
child line\_items?

```
              @ec_order = EcOrder.new(params[:ec_order])
# See if it has line items and if not, set status to

```

invalid.  
if (!@ec\_order.valid?)  
# Do something  
}

Thanks for your thoughts and ideas, - 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:** [August 13, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/how-do-i-tell-an-object-it-is-invalid/145750/2 "2008-08-13T19:23:32Z")

</div>

Hi Dave,

> and in addition to the above, I want the model to only be valid is it  
> has one or more valid :ec\_line\_items. So when creating it, can I set  
> its status to invalid if I don’t detect it has been assigned multiple  
> child line\_items?

You can write a custom validation in the model:

def validate  
errors.add(:ec\_line\_items, “is empty”) if ec\_line\_items.empty?  
end

–  
Roderick van Domburg

> **[De domeinnaam Nedforce.nl is te koop.](http://www.nedforce.nl)**
>
> Deze mooie domeinnaam kan al over een paar minuten van jou zijn. Betalen gaat eenvoudig en vertrouwd via iDEAL. Direct na de betaling ontvang je de verhuiscode per e-mail en kun je aan de slag.

---

<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:** [August 13, 2008, 9:24pm UTC](https://www.ruby-forum.com/t/how-do-i-tell-an-object-it-is-invalid/145750/3 "2008-08-13T21:24:56Z")

</div>

Hey Roderick, Thanks. This works great. One follow up I have is I  
notice when I’m redirected back to the original view because my model  
failed validation I have this line

\<% error\_messages\_for :ec\_order %\>

but nothing gets printed out. Here’s the action in the controller

```
    def summary
            # Get the subscriber corresponding to the client.
            params[:ec_order][:user_id] = session[:user_id]
            @user = User.find(session[:user_id])
            params[:ec_order][:subscriber_id] = @user.user_id
            @ec_order = EcOrder.new(params[:ec_order])
            if (params[:form] != nil)
                    for form_item_id in params[:form][:form_items]
                            @form_item =

```

FormItem.find(form\_item\_id)

@ec\_order.ec\_line\_items.build(:form\_item\_id =\> form\_item\_id,  
:description  
=\> @form\_item.description,  
:day\_supply  
=\> @form\_item.day\_supply,  
:prescription\_number  
=\> @form\_item.prescription\_number)  
end  
end  
if (!@ec\_order.valid?)  
@form = Form.find\_by\_user\_id(@user.id)  
render :action =\> ‘new’  
else  
session[:ec\_order] = @ec\_order  
end  
end

Not sure if I needed to do anything extra with the method you gave me,  
but I thought adding to the “errors” was the way to go. Thanks again  
for your help, - Dave

On Aug 13, 11:23 am, Roderick van Domburg \<rails-mailing-l…@andreas-
