# Separate validation file for each model

**URL:** <https://www.ruby-forum.com/t/separate-validation-file-for-each-model/189955>\
**Category:** Rails\
**Created:** [June 10, 2010, 3:33pm UTC](https://www.ruby-forum.com/t/separate-validation-file-for-each-model/189955 "2010-06-10T15:33:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krunal\_shah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@krunal\_shah](https://www.ruby-forum.com/u/krunal_shah)\
**Post date:** [June 10, 2010, 3:33pm UTC](https://www.ruby-forum.com/t/separate-validation-file-for-each-model/189955/1 "2010-06-10T15:33:14Z")

</div>

I have a too many models and want to maitain separate validation and  
relationship files for each model. Is there any way i can maintain it  
with rails?  
Any specific advantage to do it?

---

<div class="post-metadata">

**Author:** ![krunal\_shah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@krunal\_shah](https://www.ruby-forum.com/u/krunal_shah)\
**Post date:** [June 10, 2010, 5:43pm UTC](https://www.ruby-forum.com/t/separate-validation-file-for-each-model/189955/2 "2010-06-10T17:43:24Z")

</div>

You’d use a mixin.

El 10/06/10 15:33, Krunal Shah  
escribió:\> I have a too many models and want to maitain separate validation and

---

<div class="post-metadata">

**Author:** ![krunal\_shah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@krunal\_shah](https://www.ruby-forum.com/u/krunal_shah)\
**Post date:** [June 10, 2010, 7:11pm UTC](https://www.ruby-forum.com/t/separate-validation-file-for-each-model/189955/3 "2010-06-10T19:11:02Z")

</div>

Krunal Shah wrote:

> I have a too many models and want to maitain separate validation and  
> relationship files for each model.

How will that change the fact that you have too many models?

> Is there any way i can maintain it  
> with rails?

Use mixins or subclasses.

> Any specific advantage to do it?

Explain more about your use case.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![krunal\_shah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@krunal\_shah](https://www.ruby-forum.com/u/krunal_shah)\
**Post date:** [June 10, 2010, 8:47pm UTC](https://www.ruby-forum.com/t/separate-validation-file-for-each-model/189955/4 "2010-06-10T20:47:46Z")

</div>

Krunal Shah wrote:

> # file foo.rb
> 
> class Foo \< ActiveRecord::Base  
> include FooMethods
> 
> # — validations —
> 
> # — relationships —
> 
> belongs\_to :foobar  
> has\_many :bars
> 
> # — Class Methods —
> 
> def Foo.a\_method\_name(id)  
> …  
> end  
> end
> 
> ```auto
> 
> # file foo_methods.rb
> 
> module FooMethods
> 
> def method1
> ...
> end
> 
> def method2
> ...
> end
> 
> end
> 
> If i will create a new module for every model to separate my validation 
> and logic part. Than will it help anyway in load and other things?
> 
> ```

Don’t bother. It’s a completely pointless idea. Splitting your models  
into 2 files will provide no performance or architectural benefits that  
I can see, and will make your code harder to read.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![krunal\_shah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@krunal\_shah](https://www.ruby-forum.com/u/krunal_shah)\
**Post date:** [June 10, 2010, 8:04pm UTC](https://www.ruby-forum.com/t/separate-validation-file-for-each-model/189955/5 "2010-06-10T20:04:21Z")

</div>

# file foo.rb

class Foo \< ActiveRecord::Base  
include FooMethods

# — validations —

# — relationships —

belongs\_to :foobar  
has\_many :bars

# — Class Methods —

def Foo.a\_method\_name(id)  
…  
end  
end

```auto

# file foo_methods.rb

module FooMethods

  def method1
    ...
  end

  def method2
   ...
  end

end

If i will create a new module for every model to separate my validation 
and logic part. Than will it help anyway in load and other things?
```
