iie labs

Active Resource to Sinatra DataMapper backend

Posted over 10 years ago by Philip Ingram

In my rails app, I’m using Active Resource to ‘talk to’ another Sinatra app that is using Datamapper as it’s ORM. (To learn more about ActiveResource, check out the railscasts 94/95

I didn’t see any google fodder when i started this so I just wanted to report that it works like a charm.

The magic lies in responding with a to_xml call in Sinatra. How that data is compiled doesn’t seem to matter.

Active Resource follows the RESTful Resource mentality. By this, I mean, that when you are expecting more then ONE record back, you have to use an INDEX type action.

This kind of blew me away as I didn’t expect it to be that way.


Let’s pretend you wanted to find all posts created today.
In Active RECORD you would do:

Post.find(:all, :conditions => {:created_at => Date.today})

in Active Resource it would be:

Post.find(:all, :params => {:created_at => Date.today})

and in your Sinatra app, you would have the code:

get '/posts.xml' do
  @posts = Post.all(:created_at => params[:created_at])
  return @posts.to_xml

If you just needed to get the title of ONE post, you would fetch just that record from say /posts/:id.xml for example.


blog comments powered by Disqus