Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Model to use API instead of Database

I'm refactoring a rails app that currently uses a database and ActiveRecord so that instead of of the database, it uses a JSON API (Sinatra).

What would be the best way to refactor the models? The API will return all the same information as the database used to. Infact it IS the same database, but for scalability and multiple apps requiring access to the data, we needed to turn it into an API. Ideally only the Models will need to change and ideally I can keep some validation in the rails app.

How can I change the models so they retrieve and save their instances via API calls rather than ActiveRecord database calls?

like image 528
Greg Olsen Avatar asked Aug 24 '12 17:08

Greg Olsen


2 Answers

I think you are looking for something like ActiveResource

like image 152
Dom Barker Avatar answered Sep 19 '22 14:09

Dom Barker


For Rails 4 checkout this:

https://github.com/whichdigital/active-rest-client

This gem is for accessing REST services in an ActiveRecord style. ActiveResource already exists for this, but it doesn't work where the resource naming doesn't follow Rails conventions, it doesn't have in-built caching and it's not as flexible in general.

like image 21
0bserver07 Avatar answered Sep 19 '22 14:09

0bserver07