Is there a framework that allows me to define a model only once?

Problem

I just finished creating several models and a had to separately write all of their attributes in 1) a Rails ActiveRecord 2) a Rails database migration, and 3) a Backbone.js model. I'm not feeling very DRY. I assume the first step in DRYing up this problem would be to switch to node.js where I can use CoffeeScript on the back and front end (ideally reusing that same Backbone.js model), but what about the database schema? I realize I can create a framework that generates SQL from model attributes, but before I embark on that endeavor I was wondering if something like this already exists or is in development.

It would also be awesome if this framework could unify views and controllers so that I don't have app/views, app/controllers, app/assets/javascripts/views, app/assets/javascripts/routers, and the like all defining similar things in different places.

Problem courtesy of: Casey Foster

Solution

Check out Derby, a node.js framework under active development, which tries to unify views and controllers (and a lot more):

[Derby] runs the same code in servers and browsers, and it syncs data automatically. Derby takes care of template rendering, packaging, and model-view bindings out of the box. Since all features are designed to work together, no code duplication and glue code are needed.

I'm building a small hobby app with Derby and so far it's been fun and mind-challenging. A unified new world has its own physics..

One thing to note is Derby is built on top of Racer, a "realtime model synchronization engine"; you may find it 'heavy' if you were expecting something like a light glue code that stitches together Backbone.js, express and some database layer.


Edit: other frameworks I haven't yet looked into closely:

Solution courtesy of: ento

Discussion

It's one of goal of Wakanda system.

Discussion courtesy of: shingara

For the models, you can simply create a base class an inherit from it. You just need to call set_table_name in the derived classes. I have gotten away with this. It has the advantage that you can tweak functionality in the derived classes. Example:

app/models/widget.rb:

class Widget < ActiveRecord::Base
   def some_common_method
      # ...
   end
end

app/models/blue_widget.rb:

class BlueWidget < Widget
   set_table_name "blue_widgets"
   # special methods, if any
end

etc.

For the migrations, just define a function for the common table columns. For example:

def common_table_columns(t)
   t.integer :id
   t.stirng :some_string
   # etc
end

create_table(:table1) { |t| common_table_columns(t) }

[:table2,:table3,:table4].each do |name|
   create_table(name) { |t| common_table_columns(t) }
end

create_table :table5 do |t|
    common_table_columns(t)
    t.string :extra
end

Can't speak for Backbone.js (just now) but I am 100% confident that you can achieve the same kind of duplicate code elimination. Javascript has just as much dynamicness as ruby.

Having said all that, why DO you have several identical tables? Are you using the right solution to the right problem?

Discussion courtesy of: Michael Slade

This recipe can be found in it's original form on Stack Over Flow.