Skip to content

CouchRest::ExtendedDocument Basic Queries

newacct edited this page Sep 13, 2010 · 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