# Model validates against system values table

**URL:** <https://www.ruby-forum.com/t/model-validates-against-system-values-table/129128>\
**Category:** Rails\
**Created:** [February 14, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/model-validates-against-system-values-table/129128 "2008-02-14T18:10:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 14, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/model-validates-against-system-values-table/129128/1 "2008-02-14T18:10:16Z")

</div>

Assume that model/table “party” contains a column “party\_type” and  
consider that “party\_type” is value constrained to those values in  
model/table “system\_values” having EAV columns of table\_name,  
table\_column\_name, value\_as\_char and having a composite index  
“system\_value\_lookup\_idx” defined as:

add\_index(:system\_values,  
:table\_name,  
:table\_column\_name,  
:value\_as\_char,  
{ :name =\> :system\_value\_lookup\_idx, :unique =\> true }

Is it advisable to place a validation lookup into the “party” model?

Should one do this using the generic validate method or is there a  
better way?

Is the following a valid and useful approach?

class Party \<\< ActiveRecord::Base

validate :check\_party\_type

def check\_party-type  
errors.add-to\_base(“Party type #{self.party\_type} is not found.”)  
unless  
SystemValue.exists?(:table\_name =\> :parties,  
:table\_column\_name =\> :party\_type,  
:value\_as\_char =\> self.party\_type.to\_s  
)  
end

Comments most welcome.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 15, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/model-validates-against-system-values-table/129128/2 "2008-02-15T16:56:32Z")

</div>

James B. wrote:

> Comments most welcome.

Anyone?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 15, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/model-validates-against-system-values-table/129128/3 "2008-02-15T20:15:43Z")

</div>

James B. wrote:

> class Party \<\< ActiveRecord::Base
> 
> validate :check\_party\_type
> 
> def check\_party-type  
> errors.add-to\_base(“Party type #{self.party\_type} is not found.”)  
> unless  
> SystemValue.exists?(:table\_name =\> :parties,  
> :table\_column\_name =\> :party\_type,  
> :value\_as\_char =\> self.party\_type.to\_s  
> )  
> end
> 
> Comments most welcome.

Your approach is perfectly valid according to me, the only other option  
I can think of is to have a db trigger that checks the field against  
another table on insert but then you will not get the nicely formated  
error string that you are producing now.

hth

ilan
