Simon H. wrote:
Brilliant! Great direction for you to go in my opinion. Will Mongoose be
compatible with rails at some point?
Boy! I could write a book about this question! I don’t mean to be
flippant, but it all depends on what you mean by “compatible”.
Warning I am not a Rails guru (I’m not a Ruby guru, for that
matter). I’ve built a few Rails apps, but I am definitely not an
expert, especially on the internals of Rails. That being said…***
If you mean, “Will there ever be an ActiveRecord adapter for
Mongoose?”. Hmm, that’s a toughie. ActiveRecord really likes dbms’s
that use SQL and that implement the relational model. Assaph M., the
author of Pimki, actually wrote an ActiveRecord adapter for KirbyBase
for Rails 1.0, called Ackbar. He had to go through quite a few code
gyrations to map Rails SQL proclivities to KirbyBase’s query language.
It worked, but I don’t know how maintainable it was. Assaph himself
indicated that as ActiveRecord changed, if it broke Ackbar, it might be
too much trouble to maintain. And indeed, when Rails 1.1 came out, the
current version of Ackbar broke.
Now, Mongoose’s api is much more ActiveRecord-like than KirbyBase’s
interface. Would that make it easier to write an ActiveRecord adapter
for Mongoose (possible code name: Rikki
)? I don’t know enough
about writing ActiveRecord adapters to say. It might be an ironic
experience, since you would possibly have to go from ActiveRecord’s
interface, to some quasi-SQL translation in the guts, back to an
ActiveRecord-like api in Mongoose. Which leads me to my next point…
If you mean, “Will I be able to use Mongoose as a database backend for
Rails with reasonable success?”. Ahh, now you are on to something!
Recently, I was able to get KirbyBase functioning as a Rails back end,
simply by having my Rails models not inherit from ActiveRecord::Base.
Sure, I had to write my own #find method, etc. But it worked. I was
able to have it go all the way through the controller to the view and
show up in the scaffold-generated code. I didn’t pursue it much
further, because I would have had to write a whole ActiveRecord-like
frontend for KirbyBase in the model. Also, I noticed that the nice
error objects that are attached to your model didn’t, of course, work,
since I hadn’t subclassed ActiveRecord::Base.
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.
So, to, hopefully, answer your question. I think it is very possible to
be able to use Mongoose as a Rails backend, with the probable loss of
some functionality. Who knows, maybe in the future, ActiveRecord won’t
be as tied to SQL and it will be much easier to write adapters for
non-SQL dbmss.
See I told you I could write a book about this!
Jamey