I would like to use ActiveRecord in my Rails application, but I would like to encode data restrictions in a PostgreSQL database, not in ActiveRecord models.
For example, if I have a model created by a scaffold,
rails generate scaffold User name:string email:string yearofbirth:integer
I want the name to be long enough. eg
class User < ActiveRecord::Base validates :name, :length => { :maximum => 140 } validates_uniqueness_of :name validates_numericality_of :yearofbirth, :greater_than_or_equal_to => 1900 end
These checks are good because ActiveRecord gives some good error messages to the view if you are trying to add bad data.
However, I would like to encode the rules in the database itself and create a database with the same nice and informative ActiveRecord error messages that will then pass them to the presentation layer. For instance,
create table "Users" ( id integer unique, name varchar(140) unique check(length(name)<140), email varchar(255), yearofbirth integer check (yearofbirth >= 1900) );
What do I need to do in PostgreSQL or ActiveRecord for this to happen? When I tried it with the pgAdmin III tool, it gave error messages, but I would like to configure them.
Also, is there a way to encode these database rules directly into database migration files in Ruby?
I appreciate any help you can give me.