0

사실은 내가 두 모델 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]} 

답변

0

제공된 모델을 사용하면보기 쿼리가 간단한 예제와 함께 작동합니다.

테스트/단위/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