Skip to content

Instantly share code, notes, and snippets.

@notch8-old
Created October 23, 2008 01:05
Show Gist options
  • Select an option

  • Save notch8-old/18882 to your computer and use it in GitHub Desktop.

Select an option

Save notch8-old/18882 to your computer and use it in GitHub Desktop.
require 'couchrest'
CouchRest::Model.default_database = CouchRest.database!("http://localhost:5984/test")
class CategoryList < CouchRest::Model
key_accessor :categories
cast :categories, :as => ["Category"]
end
class Category < CouchRest::Model
key_accessor :name
key_accessor :categories
# cast :categories, :as => ["Category"]
view_by :name,
:map =>
"function(doc) {
if (doc['couchrest-type'] == 'CategoryList' && doc.categories) {
doc.categories.forEach(function(category){
emit(category.name, category);
});
}
}"
end
@category = Category.new(:name => "Biology")
@sub_category = Category.new(:name => "Sub Biology")
@category_list = CategoryList.new(:categories => [@category])
@category_list.save
categories = Category.by_name(:key => "Biology")
puts categories.inspect
puts
puts "#{categories.first.class.inspect} <- Should be Category"
puts
puts "#{categories.first.name.inspect} <- Should be the string 'Biology'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment