From: Daniel Sheppard Date: 2006-07-21T12:37:19+09:00 Subject: Re: Mongoose 0.1.1 > But, this limited success leads me to believe that I might be *much* > more successful using Mongoose with Rails, because Mongoose > is going to > stick very close to the ActiveRecord api. So, it might be as easy as > having your model classes subclass Mongoose::Table, instead of > ActiveRecord::Base. Things that wouldn't work would be stuff like > migrations, I think. Also, it would be great if I could > figure out how > to get the integrated errors working like the ActiveRecord > stuff does. > I haven't really looked at that. You've aroused my curiosity. Had a little bit of trouble figuring out how to use mongoose (some example code would have been nice), but the following gets some validation going. I couldn't get validates_presence_of to work for some reason, but here's validates_numericality_of working. Putting together a Mongoose::ActiveTable that has most of the functionality of an activerecord doesn't look to be that difficult. If implementations of the rest of the methods on ActiveRecord::Base were added, you'd be able to use validations and the other activerecord modules. require 'mongoose' require 'active_support' require 'active_record/base' require 'active_record/validations' require 'mongoose' require 'active_support' require 'active_record/base' require 'active_record/validations' class Mongoose::ActiveTable < Mongoose::Table RecordNotSaved = ActiveRecord::RecordNotSaved def update_attribute(name, value) send(name.to_s + '=', value) save end def new_record? @id.nil? end def self.human_attribute_name(attribute_key_name) #:nodoc: attribute_key_name.humanize end def save! save || raise(RecordNotSaved) end def self.init_column(col_name, col_def, col_class) super meth = <<-END_OF_STRING def #{col_name}_before_type_cast #{col_name} end END_OF_STRING self.class_eval(meth) end include ActiveRecord::Validations end class Monkey < Mongoose::ActiveTable validates_numericality_of :arms end Mongoose::Table.db = Mongoose::Database.new(:path => 'c:/temp/mongoose') unless Mongoose::Table.db.table_exists?('monkey') Mongoose::Table.db.create_table('monkey') do |t| t.add_column 'arms', :integer end end m = Monkey.new() m.arms = '2a' m.save!