Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

CouchRest::ExtendedDocument Basic Queries

newacct edited this page · 3 revisions

Model example

class Post < CouchRest::ExtendedDocument
  use_database DB

  property :title
  property :content

  view_by :title

end

By All

Load all documents that have the “couchrest-type” field equal to Post

@all_posts = Post.all
p @all_posts

Count

Returns the number of documents that have the “couchrest-type” field equal to Post

@count_posts = Post.count
puts @count_posts

First

Load the first document that have the “couchrest-type” field equal to Post

@first_post = Post.first
p @first_post

Get

Load a document from the database by id

#No exceptions will be raised if the document isn't found
@post = Post.get("some_doc_id")
p @post

#An exception will be raised if the document isn't found
@post = Post.get!("some_doc_id")
p @post

Using Views

See couchrest/mixins/views.rb for more details

#Load all documents that have the "couchrest-type" field equal to Post and field title
@posts = Post.by_title
puts "#{@posts.count} posts was found"
puts
p @posts

#Load all documents that have the "couchrest-type" field equal to Post and "a specific title"
@posts = Post.by_title(:key => "A specific title")
p @posts
Something went wrong with that request. Please try again.