I had a possibly new spin on this same issue. How about specially
encoding the model<-->DB definitions into specially formatted comments
in the ActiveRecord model? The purpose here is:
* give me one place to figure out the definition of a given model (the
relationships, the methods, and the data members as well)
* let me stay away from raw SQL, if possible
* stay DRY
So, how about:
---
model Article < ActiveRecord::Base
#:= title varchar(64)
#:= description varchar(255)
#:= rating int(5)
has_many :authors
def some_method
...
end
end
---
Above, "#:=" is treated as comment, but it can also be parsed out as a
meta-definition for SQL usage. Once I define my models, I could run a
script like:
./script/model2db Article
which would look at the model definition, parse for the DB
definitions, and inject that into the schema (possibly altering the
table if it already exists). It seems DRY enough. What do you all
think?
-d
David Heinemeier Hansson
2005-Apr-14 21:46 UTC
Re: Mixing the Model & DB definitions (again)
> which would look at the model definition, parse for the DB > definitions, and inject that into the schema (possibly altering the > table if it already exists). It seems DRY enough. What do you all > think?Look in the archive for discussions on Migrations. I describe the way these definitions will eventually find their way into AR. But as real code, not comments. -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://www.loudthinking.com/ -- Broadcasting Brain