# (Non-active Record) Models and inheritance

**URL:** <https://www.ruby-forum.com/t/non-active-record-models-and-inheritance/126925>\
**Category:** Rails\
**Created:** [January 23, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/non-active-record-models-and-inheritance/126925 "2008-01-23T14:15:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f14d63/32.png) [@dara](https://www.ruby-forum.com/u/dara)\
**Post date:** [January 23, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/non-active-record-models-and-inheritance/126925/1 "2008-01-23T14:15:05Z")

</div>

I have a non-persisted model for a web form. Model is taking care of  
business logic without mapping directly to a DB.

I have included validation for the form model by creating the following  
Module :

module Validation::ModelValidation  
[:save, :save!, :update\_attribute].each{ |attr| define\_method(attr){}  
}

def method\_missing(symbol, _params)  
if(symbol.to\_s =~ /(._)\_before\_type\_cast$/)  
send($1)  
end  
end

def self.append\_features(base)  
super  
base.send(:include, ActiveRecord::Validations)  
end

end

and including in my model with:

class MyValidatedModel  
include Validation::ModelValidation

…  
end

In ‘MyValidatedModel’ I also have needed to add the method

def self.human\_attribute\_name(attr)  
return @@atty\_names[attr] || (attr.nil??attr:attr.humanize)  
end

for the framework to return friendly names to ‘error\_messages\_for’, and  
have my field names and corresponding names in a class hash as per :

@@atty\_names = {  
‘username’ =\> ‘User Name’,  
‘email’ =\> ‘E-Mail Address’,  
‘firstname’ =\>‘First Name’,  
‘lastname’ =\> ‘Last Name’,  
‘dob’ =\> ‘Date of Birth’,  
}

I wanted self.human\_attribute\_name(attr) to be in the module also, but  
it wouldn’t work there, so I am looking at model inheritance for models  
to achieve this.

I have other forms that I also want to use validation with.

I am thinking of creating a ValidatableModel class, with the  
human\_attribute\_name(attr) method and associated hash in it and  
including the module, and then inherit from this in all the  
Non-active-record models I want to be validatable.

I am just wondering if there is a better way to solve this, and what are  
the conventions for model inheritance for non-persisted models in rails.

So far I have only found discussions relating to ActiveRecord persisted  
models online.

Still looking…

---

<div class="post-metadata">

**Author:** ![dara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f14d63/32.png) [@dara](https://www.ruby-forum.com/u/dara)\
**Post date:** [January 23, 2008, 2:27pm UTC](https://www.ruby-forum.com/t/non-active-record-models-and-inheritance/126925/2 "2008-01-23T14:27:01Z")

</div>

Small point on error,

replace

> def self.human\_attribute\_name(attr)  
> return @@atty\_names[attr] || (attr.nil??attr:attr.humanize)  
> end

with

def self.human\_attribute\_name(attr)  
return @@atty\_names[attr] || attr.humanize  
end

if you are duplicating this as the ternary operation was unneccessary

---

<div class="post-metadata">

**Author:** ![dara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f14d63/32.png) [@dara](https://www.ruby-forum.com/u/dara)\
**Post date:** [January 23, 2008, 2:27pm UTC](https://www.ruby-forum.com/t/non-active-record-models-and-inheritance/126925/3 "2008-01-23T14:27:54Z")

</div>

On 23 Jan 2008, at 13:15, Dara Ca wrote:

> models  
> to achieve this.  
> The classic way of doing this is to have a ClassMethods module  
> containing the class methods you want to add.  
> You can then to base.extend(ClassMethods) from self.included (or since  
> from append\_features since you’re use that)

Fred
