Rails could be DRYer
I'm playing with Ruby on Rails these days and I've been thinking about how Rails isn't entirely DRY. (i.e., doesn't implement the Don't Repeat Yourself philosophy fully.) The area where it falls short of DRY is in the duplication between defining the database schema (either manually, or through the use of migrations) and defining the Model.
In Rails you can define constraints and validations in the Model. You repeat the Model definition when you create migrations to create your database structure (or if you create you create your schema manually, when you do that.) You repeat the constraints (e.g., foreign keys) in the database also. For Rails to be completely DRY, it should generate the schema and update the database automatically based on the current state of your Model classes. (Allowing overrides that go low-level, where necessary, of course for specific optimizations or advanced SQL.)
Googling around, I came across this discussion of a suggestion for ActiveSchema where people are discussing the same thing.
Comments
by Thom Shannon on 2006-10-10 23:13:49
by Nick on 2006-10-11 07:18:33
by Martin on 2006-10-11 07:19:53
by Dan Glegg on 2006-10-11 09:09:41
by aral on 2006-10-11 09:33:37
class Author /regex/ end end # [...] class Post 3..100 end attribute :summary, :text attribute :content, :text end
i did a small investigation and concluded that this could never be implemented inside (or as some sort of extention/plugin/etc to) ActiveRecord, because of technical difficulty and because of the rails not seeing the unDRYness as a problem. that's why: ManagedRecord will be needed. i have no clue if Managed- and ActiveRecord could cooperate, of it it would be possible to maintain compatibillity with AR plugins. my guess: mostly likely not. ManagedReocrd will put a lot more stress on the implementation of the database interface, it might also enable the developer to use stuff like database-side referential integrity (and other database features) with very little efford. important: what happends if a ManagedRecord model is no longer matching the db? - in development: an exception should be raised, and catched by the framework to start the auto generation and execution of a migration (this could be accomplished by a web based interface) - in production: this should never happen! when the models are updated on a production server the migrations also have to update. pfff.... long comment, i hope some one reads it. with questions, or further investigations: please mail me (cies,at;kde:dot.nl). Cies Breijsby cies on 2006-10-11 11:51:46
by cies on 2006-10-11 11:56:20
by cies on 2006-10-11 12:18:41
by aral on 2006-10-11 16:00:58
by giles on 2006-10-15 02:36:20