What is the difference between ActiveResource and ActiveModel?
Asked Answered
T

2

14

As a preface to this question: I am brand new to Rails development (and web-development in general), and some of my concerns may be unfounded, so any feedback would be very helpful.

I am attempting to make a Rails application that plugs into a RESTful API. I have been trying to determine the best way to go about this, and from what I understand, its narrowed down to making my own model from the ground up, utilizing ActiveModel, or utilizing ActiveResource.

It is unclear to me the advantages/disadvantages of each, and to be frank, I do not yet fully understand the difference between ActiveModel and ActiveResource. Can somebody provide me with insight regarding these three options and what makes the most 'sense' in a ror context? Thanks!

The best answer wouldn't just say "Use ActiveModel", or "Use ActiveResource" with instructions on doing so, however that would be helpful as well. I would really appreciate an answer explaining why I should use that thing, etc.

A few constraints I am dealing with are that I need to be using a key when I call the API, and a good number of the API calls will contain additional parameters.

Topeka answered 15/6, 2012 at 23:50 Comment(0)
R
21

So the key to choosing which package to use here is whether:

  1. You are RETRIEVING data from the web API and want to store/manipulate on YOUR server, or
  2. You are MANIPULATING data via the web API and don't intend on storing anything on your server.

If #1, you'll need ActiveRecord, as it's Rails' package for manipulation and storage of data on your Postgres/MySQL/etc database.

If #2, you can use ActiveResource exclusively, which will let you retrieve data from the web API, work with it at runtime, and then make changes by posting back to the web API.

Many applications though, will often use both of these packages. ActiveResource to grab data very easily, and then applying it to ActiveRecord models (like User, or Location, etc) which you can use locally without having to grab data from the API over and over again.

To give you an example, for a service I was working on I grabbed Geolocation data from a public source (looking up coordinates for zipcodes), I then saved that data to local Location objects using ActiveRecord so I could look them up repeatedly without the delay of the Web API call. (if you're smart, you'll refresh this data from the web API from time to time)

Determining whether ActiveResource will work for you

Do the service requests conform to the documentation protocol? Look at the Expects a response of block in the Find method, for example. If so, you might be good to go without extra work.

Note: The documentation is a little out of sync with the changelog — as of Rails 3.1:

The default format has been changed to JSON for all requests. If you want to continue to use XML you will need to set self.format = :xml in the class. eg.

Also, ActiveResource has been removed entirely from the Rails 4.0 branch, so if you're looking forward to starting a new rails application and want the latest and greatest, this isn't an option at all — but all hope is not lost, there are plenty of gems that make interacting with RESTful interfaces simpler, like Faraday (full disclosure: I have not used faraday myself so can really comment on its efficacy, but I located it here, and there are a number of other options.

Note (from the same link above): Active Resource is built on a standard XML format for requesting and submitting resources over HTTP. It mirrors the RESTful routing built into Action Controller but will also work with any other REST service that properly implements the protocol. REST uses HTTP, but unlike “typical” web applications, it makes use of all the verbs available in the HTTP specification.

If the answer to the above is no (it does not conform) you'll need to write a wrapper class, see Facebooker for an example of how this is done in an actively maintained gem.

References: Great tutorial on ActiveResource

Getting started with ActiveRecord

Keep in mind if you're just starting web development, you'll need to understand database and model basics too — you have your work cut out for you. :)

Rushy answered 18/6, 2012 at 16:35 Comment(7)
To answer your question, I will be manipulating data through the API, there is no need for my application to store its own data. If you remove that response from your answer, that would make it more clear. Also, your answer contains no mention of ActiveModel. Where does this fit into the picture?Topeka
ActiveModel and ActiveRecord work together, ActiveModel is used to abstract / describe your data on the database, ActiveRecord is the QUERY INTERFACE for retrieving data that is IN your Models from the database. Sorry, I think of them interchangeably since you won't normally use one without the other.Rushy
That is great to know! Thanks for the clarification. So it sounds like if anything, I would be using ActiveResource, correct?Topeka
Looks that way — and I could remove the part you suggest from the answer, but I think it's actually an important differentiation to understand (store vs. no store), no?Rushy
Yeah I would agree. I think the answer could benefit from further explanation regarding ActiveResource. What specs must the API adhere to in order for ActiveResource to be able to consume it? What if the API doesn't adhere to those specs? Would it make sense to ditch Rails or to write my own classes?, etc.Topeka
Great answer! I have heard, however that ActiveResource is being deprecated from Rails 4. If this is true, I think it makes sense to not use it. Also, I am a little unclear as to what you are saying about XML. Is that to say that if the API responds with some other format, such as JSON, ActiveResource is automatically eliminated? An updated answer including these clarifications will earn you the bounty.Topeka
Here's more clarification on ActiveResource & Rails 4 : yetimedia.tumblr.com/post/35233051627/…Factorage
D
1

I will give it a try, and hopefully others will come and correct (or add) to it, so a better picture will exist ...

I see the following main differences between the two:

  • ActiceResource provides an interface to a resource that is (normally) accessible remotely by a (Rails) RESTful API. It is not stored kept locally, but read, updated, created and deleted by the API only. As Ryan Bates states it: "ActiveResource allows you to easily communicate between multiple Rails applications."
  • ActiveRecord (or nowadays ActiveModel) stores its record (or model) in a database locally, and allows others to access it remotely by a web interface for
    • Showing pages
    • Returning JSON maps or
    • Returning XML structures

Advantages and Disadvantages

To use ActiveResource, you have to check that your local Rails app can speak to the remote application, so that the RESTful API is compatible.

If both options are free to choose, here are some arguments:

  • ActiveResource will normally more expensive (calling a remote system costs at least more time) than ActiveModel.
  • ActiveModel will cost your own resources (setup of database at least), but this is normally not a burden.

So at the end, it depends on if you want to store something (ActiveModel) or only retrieve something (ActiveResource), which means you will use ActiveResource and (perhaps) ActiveMiodel.

Delegate answered 18/6, 2012 at 16:41 Comment(3)
So it sounds like you are saying that ActiveModel is the same thing as ActiveRecord? There is no chance of me storing the data locally, so it sounds like the way to go is going to be ActiveResource. For ActiveResource, I keep reading that it must be a (Rails) RESTful API. The API I will be accessing is not a RAILS app. Does this mean ActiveResource is not an option?Topeka
How can I determine weather or not the API I am accessing is consumable by a Rails app? If it isn't would I be better off abandoning Rails all together?Topeka
See the reference to the RailsCast #94 with the citation at the beginning.Delegate

© 2022 - 2024 — McMap. All rights reserved.