Browse files

Updated readme for has_one relationship

  • Loading branch information...
1 parent ce93e75 commit d997547d1a9137f1aa2a7c7175eaa00018e4f2b0 jfcixmedia committed Apr 10, 2012
Showing with 16 additions and 3 deletions.
  1. +16 −3 README.md
View
19 README.md
@@ -107,7 +107,7 @@ This feature is not stable and might change in the future, probably by using a m
## Relationships
-You can define `has_many` relationships in your models. The relationship data is handled in two different ways. When parsing a resource from JSON data, if there’s a relationship data included, it will be used to create new Ruby objects.
+You can define `has_many` and `has_one` relationships in your models. The relationship data is handled in two different ways. When parsing a resource from JSON data, if there’s a relationship data included, it will be used to create new Ruby objects.
If no relationship data was included when parsing a resource, calling a method with the same name as the relationship will fetch the data (providing there’s an HTTP request available for it).
@@ -117,18 +117,24 @@ For example, with this setup:
class User
include Her::Model
has_many :comments
+ has_one :role
end
class Comment
include Her::Model
end
+
+class Role
+ include Her::Model
+end
```
If there’s relationship data in the resource, no extra HTTP request is made when calling the `#comments` method and an array of resources are returned:
```ruby
-@user = User.find(1) # { :data => { :id => 1, :name => "George Michael Bluth", :comments => [{ :id => 1, :text => "Foo" }, { :id => 2, :text => "Bar" }] }}
+@user = User.find(1) # { :data => { :id => 1, :name => "George Michael Bluth", :comments => [{ :id => 1, :text => "Foo" }, { :id => 2, :text => "Bar" }], :role => { :id => 1, :name => "Admin" } }}
@user.comments # => [#<Comment id=1>, #<Comment id=2>] fetched directly from @user
+@user.role # => #<Role id=1> fetched directly from @user
```
If there’s no relationship data in the resource, an extra HTTP request (to `GET /users/1/comments`) is made when calling the `#comments` method:
@@ -138,7 +144,14 @@ If there’s no relationship data in the resource, an extra HTTP request (to `GE
@user.comments # => [#<Comment id=1>, #<Comment id=2>] fetched from /users/1/comments
```
-However, subsequent calls to `#comments` will not trigger the extra HTTP request.
+For `has_one` relationship, an extra HTTP request (to `GET /users/1/role`) is made when calling the `#role` method:
+
+```ruby
+@user = User.find(1) # { :data => { :id => 1, :name => "George Michael Bluth" }}
+@user.role # => #<Role id=1> fetched from /users/1/role
+```
+
+However, subsequent calls to `#comments` or `#role` will not trigger the extra HTTP request.
## Custom requests

0 comments on commit d997547

Please sign in to comment.