# How do I? Same Model, Different Controllers, Different Valid

**URL:** <https://www.ruby-forum.com/t/how-do-i-same-model-different-controllers-different-valid/100386>\
**Category:** Rails\
**Created:** [February 2, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/how-do-i-same-model-different-controllers-different-valid/100386 "2006-02-02T17:12:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_on\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Josh\_on\_Rails](https://www.ruby-forum.com/u/Josh_on_Rails)\
**Post date:** [February 2, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/how-do-i-same-model-different-controllers-different-valid/100386/1 "2006-02-02T17:12:58Z")

</div>

Hi all –

The simple question is: How do I set different validations for the same  
Model, when accessed in different Controllers?

Here’s the scenario: I have a store/cart app, with a customer-facing  
shopping cart, and a typical Admin side. The Admin side also has an  
Order Entry component, where our staff can enter orders received by fax,  
telephone, etc. Both the customer-facing cart and the Admin-side Orders  
would use the same model (same underlying data) for customer information

- billing, shipping, etc.

However, the Admin side is intended to be a bit more flexible than the  
customer-facing side. For example, we might take international orders by  
phone, but not over the Web. Or we might allow payment with  
PO/check/cash by an Admin-side order, but not from a Web order.

If I set something like “validate :credit\_card\_number” in the Model, it  
will trigger on the Admin side, requiring a credit card, where I’d  
prefer to allow a blank credit card. How do I set per-Controller  
validation? Or, is there another, more idiomatic way to approach this  
altogether?

– Joshua

---

<div class="post-metadata">

**Author:** ![Josh\_on\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Josh\_on\_Rails](https://www.ruby-forum.com/u/Josh_on_Rails)\
**Post date:** [February 2, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/how-do-i-same-model-different-controllers-different-valid/100386/2 "2006-02-02T17:34:13Z")

</div>

Hi Josh,

You might want to take a look at some of the ideas in this great article  
by Scott Baron:  
[http://lunchroom.lunchboxsoftware.com/pages/specifications/](http://lunchroom.lunchboxsoftware.com/pages/specifications/)

I’m facing some similar issues in a project at the moment, and the key  
concept is shifting business-logic (or multi-model, context dependent)  
restraints out of the largely single-model validation framework in  
rails.

Hope the links provides food for thought.

Simon

---

<div class="post-metadata">

**Author:** ![Josh\_on\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Josh\_on\_Rails](https://www.ruby-forum.com/u/Josh_on_Rails)\
**Post date:** [February 2, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/how-do-i-same-model-different-controllers-different-valid/100386/3 "2006-02-02T17:58:22Z")

</div>

Josh

Using 2 related models is the simplest way :

```
class Cart < ActiveRecord::Base
   ... validations for the admin

class PublicCart < Cart
   ... validations for everybody else

```

Alain

---

<div class="post-metadata">

**Author:** ![Josh\_on\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Josh\_on\_Rails](https://www.ruby-forum.com/u/Josh_on_Rails)\
**Post date:** [February 2, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/how-do-i-same-model-different-controllers-different-valid/100386/4 "2006-02-02T20:23:37Z")

</div>

Not getting anything at that link… are you sure it’s correct?

b

---

<div class="post-metadata">

**Author:** ![Josh\_on\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Josh\_on\_Rails](https://www.ruby-forum.com/u/Josh_on_Rails)\
**Post date:** [February 7, 2006, 12:26am UTC](https://www.ruby-forum.com/t/how-do-i-same-model-different-controllers-different-valid/100386/5 "2006-02-07T00:26:24Z")

</div>

Any place besides the book he mentions (“Domain-Driven Design”) where  
I can find more background about this? It’s an interesting article but  
I can’t figure out if he intends specifications to exist solely for  
the purpose of making validation easier, or if there’s more to it than  
that. Is the goal just to decouple validation from the specification  
you’re validating against?

On 2/2/06, Simon R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hope the links provides food for thought.
> 
> > Here’s the scenario: I have a store/cart app, with a customer-facing
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Giles Goat Boy

[http://gilesmakesmusic.blogspot.com](http://gilesmakesmusic.blogspot.com)  
[http://gileswritescode.blogspot.com](http://gileswritescode.blogspot.com)

---

<div class="post-metadata">

**Author:** ![Josh\_on\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Josh\_on\_Rails](https://www.ruby-forum.com/u/Josh_on_Rails)\
**Post date:** [February 7, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/how-do-i-same-model-different-controllers-different-valid/100386/6 "2006-02-07T18:44:32Z")

</div>

Thanks Ben – I’m going through the white paper, it looks pretty good.

On 2/6/06, Ben M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> an interesting read regardless.
> 
> > you’re validating against?
> > 
> > > concept is shifting business-logic (or multi-model, context dependent)
> > > 
> > > > However, the Admin side is intended to be a bit more flexible than the
> > > 
> > > removed\_email\_address@domain.invalid
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Giles Goat Boy

[http://gilesmakesmusic.blogspot.com](http://gilesmakesmusic.blogspot.com)  
[http://gileswritescode.blogspot.com](http://gileswritescode.blogspot.com)

---

<div class="post-metadata">

**Author:** ![Josh\_on\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Josh\_on\_Rails](https://www.ruby-forum.com/u/Josh_on_Rails)\
**Post date:** [February 7, 2006, 2:03am UTC](https://www.ruby-forum.com/t/how-do-i-same-model-different-controllers-different-valid/100386/7 "2006-02-07T02:03:55Z")

</div>

He’s talking about the Specification Pattern, in which you create an  
object that handles verification of some group of  
objects requirements.

Here’s a paper by Martin F. and Eric Evans about it (Evans used a  
bunch of stuff from this in his chapter in the book):

> **[spec.pdf](https://www.martinfowler.com/apsupp/spec.pdf)**
>
> 76.67 KB

There’s also the site for the book:

[http://domaindrivendesign.org/](http://domaindrivendesign.org/)

which unfortunately never really took off, but it does have a 40-page  
.doc download that has summaries of the patterns  
in the book.

There’s also this site:

[http://patternshare.org/default.aspx/Home.DDD.HomePage](http://patternshare.org/default.aspx/Home.DDD.HomePage)

with an annoyingly small font size and short summaries of the patterns.

But basically, you should just get the book… at least if you’re going  
to be doing fairly big applications. Well, it’s  
an interesting read regardless.

b
