Model validates against system values table

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.

James B. wrote:

Comments most welcome.

Anyone?

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