# Model validation

**URL:** <https://www.ruby-forum.com/t/model-validation/158480>\
**Category:** Rails\
**Created:** [January 23, 2009, 8:22pm UTC](https://www.ruby-forum.com/t/model-validation/158480 "2009-01-23T20:22:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dowker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9f8e36/32.png) [@Dowker](https://www.ruby-forum.com/u/Dowker)\
**Post date:** [January 23, 2009, 8:22pm UTC](https://www.ruby-forum.com/t/model-validation/158480/1 "2009-01-23T20:22:16Z")

</div>

I need a little advice on how to best set things up. I have a model  
where I will have a short form and a longer form in two different  
places in my application. Obviously, I would like to apply validating  
rules to both forms. The short form is a subset of the fields in the  
model. The long form is the full model so it isn’t a problem. What are  
my options?

(1) There is functionality in the models that I don’t know about to  
handle this situation.  
(2) I should create a second model for the extra fields between the  
short and long forms, using the same database table.  
(3) I should create a second model for the extra fields between the  
short and long forms, using a different database table and a has\_one  
association.  
(4) Any other suggestions?

Thanks.

---

<div class="post-metadata">

**Author:** ![Dowker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9f8e36/32.png) [@Dowker](https://www.ruby-forum.com/u/Dowker)\
**Post date:** [January 23, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/model-validation/158480/2 "2009-01-23T21:49:42Z")

</div>

It’s kind of unclear about what you are trying to do here. Say you have  
a model like so:

class Customer \< ActiveRecord::Base  
# contains attributes :fname, :lname, :phone, :city, :state, :zip  
validates\_presence\_of :fname, :lname, :phone, :zip  
end

This model will validate the presence of the 4 fields (:fname, :lname,  
:phone, :zip). Would this be comparable to the “longer” version?

If so, would the shorter version be required to ONLY validate a subset  
of the validates\_presence\_of list? Perhaps only :fname & :lname?

If this is the case you can just set the default validations to the  
short list like so:

```
validates_presence_of :fname, :lname

```

In order to get the long validation, you can do this with your model:

class Customer \< ActiveRecord::Base  
# contains attributes :fname, :lname, :phone, :city, :state, :zip  
validates\_presence\_of :fname, :lname

```
@long_validation
attr_accessor :long_validation

def validate
    if @long_validation
        errors.add("phone number cannot be blank") if phone.blank?
        errors.add("zip code cannot be blank") if zip.blank?
    end
end

```

end

Test this using your script/console from the terminal.

HTH,  
Aldo S.

Dowker wrote:

> I need a little advice on how to best set things up. I have a model  
> where I will have a short form and a longer form in two different  
> places in my application. Obviously, I would like to apply validating  
> rules to both forms. The short form is a subset of the fields in the  
> model. The long form is the full model so it isn’t a problem. What are  
> my options?
> 
> (1) There is functionality in the models that I don’t know about to  
> handle this situation.  
> (2) I should create a second model for the extra fields between the  
> short and long forms, using the same database table.  
> (3) I should create a second model for the extra fields between the  
> short and long forms, using a different database table and a has\_one  
> association.  
> (4) Any other suggestions?
> 
> Thanks.

---

<div class="post-metadata">

**Author:** ![Dowker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9f8e36/32.png) [@Dowker](https://www.ruby-forum.com/u/Dowker)\
**Post date:** [January 23, 2009, 9:53pm UTC](https://www.ruby-forum.com/t/model-validation/158480/3 "2009-01-23T21:53:06Z")

</div>

Ack!

Forgot to mention, that now you can do this in the controller when  
loading the ActiveRecord object:

def some\_action  
@customer = Customer.find\_by\_id(1) #however you are loading the  
object  
@customer.long\_validation = true  
# now you can use @customer in your form\_for @customer in the view!  
end

HTH,  
Aldo S.

Aldo S. wrote:

> It’s kind of unclear about what you are trying to do here. Say you have  
> a model like so:
> 
> class Customer \< ActiveRecord::Base  
> # contains attributes :fname, :lname, :phone, :city, :state, :zip  
> validates\_presence\_of :fname, :lname, :phone, :zip  
> end
> 
> This model will validate the presence of the 4 fields (:fname, :lname,  
> :phone, :zip). Would this be comparable to the “longer” version?
> 
> If so, would the shorter version be required to ONLY validate a subset  
> of the validates\_presence\_of list? Perhaps only :fname & :lname?
> 
> If this is the case you can just set the default validations to the  
> short list like so:
> 
> ```
> validates_presence_of :fname, :lname
> 
> ```
> 
> In order to get the long validation, you can do this with your model:
> 
> class Customer \< ActiveRecord::Base  
> # contains attributes :fname, :lname, :phone, :city, :state, :zip  
> validates\_presence\_of :fname, :lname
> 
> ```
> @long_validation
> attr_accessor :long_validation
> 
> def validate
> if @long_validation
> errors.add("phone number cannot be blank") if phone.blank?
> errors.add("zip code cannot be blank") if zip.blank?
> end
> end
> 
> ```
> 
> end
> 
> Test this using your script/console from the terminal.
> 
> HTH,  
> Aldo S.
> 
> Dowker wrote:
> 
> > I need a little advice on how to best set things up. I have a model  
> > where I will have a short form and a longer form in two different  
> > places in my application. Obviously, I would like to apply validating  
> > rules to both forms. The short form is a subset of the fields in the  
> > model. The long form is the full model so it isn’t a problem. What are  
> > my options?
> > 
> > (1) There is functionality in the models that I don’t know about to  
> > handle this situation.  
> > (2) I should create a second model for the extra fields between the  
> > short and long forms, using the same database table.  
> > (3) I should create a second model for the extra fields between the  
> > short and long forms, using a different database table and a has\_one  
> > association.  
> > (4) Any other suggestions?
> > 
> > Thanks.

---

<div class="post-metadata">

**Author:** ![Dowker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9f8e36/32.png) [@Dowker](https://www.ruby-forum.com/u/Dowker)\
**Post date:** [January 23, 2009, 10:20pm UTC](https://www.ruby-forum.com/t/model-validation/158480/4 "2009-01-23T22:20:13Z")

</div>

Thanks Aldo. That was exactly what I was trying to communicate. Sorry  
if I was not clearer.

On Jan 23, 3:49 pm, Aldo S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
