Question

Our Rails3 app retrieves all data from a REST server (e.g. for create/update/delete/find ..), what abstraction should we build so that we can convert the JSON output to a object on the Rails side. Does it make sense to use a ActiveRecord object in this case, it seems to be little heavyweight since we will not perform any database persistence calls using the ActiveRecord object. What design paradigm should I use?

Was it helpful?

Solution

That's a really difficult question to answer as you have provided very little information on how you want to use/manipulate the data you have consumed however I think a good pointer might be for you to have a close look at ActiveModel.

Check out the rails docs -> http://apidock.com/rails/Rails/Generators/ActiveModel

Check out the awesome RailsCasts -> http://railscasts.com/episodes/219-active-model

This will give you the same api as ActiveRecord but without the database thus enabling you to manipulate your data in a way that is intuitive to Rails development.

I would have suggested using ActiveResource -> http://apidock.com/rails/ActiveResource/Base. It is a superb way to consume a REST API in Rails but this has been deprecated as of Rails v4 and probably would not be suitable moving forward. Although it has been extracted out into a gem and was only removed from the Rails core as development was stagnant. The gem has apparently been given some love so you may wish to go with this.

More info here http://yetimedia.tumblr.com/post/35233051627/activeresource-is-dead-long-live-activeresource and of course the inevitable Railscasts -> http://railscasts.com/episodes/94-activeresource-basics and http://railscasts.com/episodes/95-more-on-activeresource

Hope that helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top