不使用MongoMapper范围的投掷的错误列表数据<勇敢的::查询状态:“活动”>
问题描述:
其实我已经创建使用mongomapper概念两型我为主动获取作者,但是像 不使用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