The DrySQL library?? goes a long way toward removing this duplication. It infers the
schema relationships and validation rules from the database??™s types and constraints, so
they don??™t have to be specified in the application. DrySQL works with all of the major
DBMSs: PostgreSQL 8 and up, MySQL 5 and up, SQL Server, Oracle, and DB2.
With DrySQL installed, you can simply require the library in the environment configuration
file:
require 'drysql'
Then, all that is needed is to inform ActiveRecord of the mapping between tables and
model classes (even that is not necessary if the tables are named according to the
defaults):
class Client
set_table_name "customers"
end
* I would probably keep that one at the application level, because it contains the business rule that no
employee??™s salary is zero. However, ???an employee??™s salary must be non-negative??? would most likely be an
integrity constraint, as it is nearly inconceivable that you would ???pay??? an employee a negative salary.
?? http://drysql.rubyforge.org/
112 | Chapter 4: Database
If the table had been named clients, you would not even need the set_table_name call.
The relationships and constraints will be inferred from the customers table??™s constraints.
Composite Keys
Composite keys, primary keys made up of two or more attributes, are best avoided.
Not only are they harder to manage than simple primary keys, they are usually more
fragile.
Pages:
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177