Skip to content

Latest commit

 

History

History

examples

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Examples

Create Class

class Blog < ActiveRequest::Base
  attr_accessor :id, :title
end

Find All

blogs = Blog.all # "GET /v1/blogs.json HTTP/1.1" 200

Find

blog = Blog.find(1) # "GET /v1/blogs/1.json HTTP/1.1" 200

Where

blogs = Blog.where(title: "title 1") # "GET /v1/blogs.json?title=title%201 HTTP/1.1" 200

Create

blog = Blog.new(title: "test") # Blog.new("title" => "test")
blog.save # => true  | "POST /v1/blogs.json?blog[title]=test&blog[posts_attributes][]= HTTP/1.1" 201

or

blog = Blog.create(title: "Test") # "POST /v1/blogs.json?blog[title]=Test&blog[posts_attributes][]= HTTP/1.1" 201

Update

blog = Blog.find(1)
blog.title = "new title"
blog.save # "PUT /v1/blogs/1.json?blog[id]=1&blog[title]=new%20title&blog[posts_attributes][]= HTTP/1.1" 200

Delete

blog = Blog.find(1)
blog.delete # "DELETE /v1/blogs/1.json HTTP/1.1" 200

Association

classes

class Blog < ActiveRequest::Base
  attr_accessor :id, :title
  has_many :posts
end

class Post < ActiveRequest::Base
  attr_accessor :id, :title, :body
  belongs_to :blog
end

has many

blog = Blog.find 1
=> #<Blog:0x0056334e564ed8 @id=1, @title="title 1", @posts=[]>
blog.posts # "GET /v1/blogs/1/posts.json HTTP/1.1" 200
=> [#<Post:0x0056334e554240 @id=1, @title="title 1", @body="body 1", @blog=nil>, #<Post:0x0056334e5537a0 @id=2, @title="title 2", @body="body 2", @blog=nil>]

belongs to

post = Post.find 1
=> #<Post:0x007fb2c4112578 @blog_id=1, @blog=nil, @id=1, @title="title 1", @body="body 1">
post.blog # "GET /v1/blogs/1.json HTTP/1.1" 200
=> #<Blog:0x007fb2c40862d0 @posts=[], @id=1, @title="title 1">
post
=> #<Post:0x007fb2c4112578 @blog_id=1, @blog=#<Blog:0x007fb2c40862d0 @posts=[], @id=1, @title="title 1">, @id=1, @title="title 1", @body="body 1">