不使用MongoMapper范围抛出数据抛出错误<plucky :: query status:=“”“active”=“”> </plucky :: query>

时间:2014-03-28 07:05:40

标签: ruby-on-rails ruby-on-rails-3 mongodb ruby-on-rails-3.1 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]}

1 个答案:

答案 0 :(得分: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

$ rake test

运行选项:

# 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