aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source/active_resource_basics.textile
blob: 64f0949475c6a65fe9456f19c11878dd1441c204 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
h2. Active Resource Basics

This guide should provide you with all you need to get started managing the connection between business objects and RESTful web services. It implements a way to map web-based resources to local objects with CRUD semantics.

endprologue.

WARNING. This Guide is based on Rails 3.0. Some of the code shown here will not work in earlier versions of Rails.

h3. Introduction

Active Resource allows you to connect with RESTful web services. So, in Rails, Resource classes inherited from +ActiveResource::Base+ and live in  +app/models+.

h3. Configuration and Usage

Putting Active Resource to use is very similar to Active Record.  It's as simple as creating a model class
that inherits from ActiveResource::Base and providing a <tt>site</tt> class variable to it:

<ruby>
class Person < ActiveResource::Base
  self.site = "http://api.people.com:3000/"
end
</ruby>

Now the Person class is REST enabled and can invoke REST services very similarly to how Active Record invokes
life cycle methods that operate against a persistent store.

<ruby>
# Find a person with id = 1
ryan = Person.find(1)
Person.exists?(1)  # => true
</ruby>

h3. Changelog

* July 30, 2011: Initial version by "Vishnu Atrai":http://github.com/vatrai