实际上,我通过使用mongomapper概念创建了两个模型,我提到了获取活跃作者的范围,但它的抛出错误像
模型1:class Author
include MongoMapper::Document
key :name, String
key :status, String
validates_presence_of :name
many :books
scope :active, where(:status => 'Active')
end
模型2:
class Book
include MongoMapper::Document
key :title
key :author_id, ObjectId
key :status, :default => 'Active'
validates_presence_of :title
belongs_to :author
scope :active, where(:status => 'Active')
end
视图:
Author.active.collect{|a| [a.name,a.id]}
使用提供的模型,视图查询为我提供了一个简单的示例。
测试/单位/author_test.rb
require 'test_helper'
class AuthorTest < ActiveSupport::TestCase
def setup
Author.delete_all
Book.delete_all
end
test "scope active" do
puts "nMongoMapper::Version:#{MongoMapper::Version}"
Author.create(name: 'J. K. Rowling', status: 'Active')
a = Author.active.collect{|a| [a.name,a.id]}
assert_equal("J. K. Rowling", a.first.first)
p a
end
end
运行选项:
# Running tests:
[1/1] AuthorTest#test_scope_active
MongoMapper::Version:0.12.0
[DEPRECATED] The 'safe' write concern option has been deprecated in favor of 'w'.
[["J. K. Rowling", BSON::ObjectId('535966d77f11ba2632000001')]]
Finished tests in 0.058895s, 16.9794 tests/s, 16.9794 assertions/s.
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips