"不等于"使用Mongoid在rails中命名范围

Ann*_*nie 7 model-view-controller ruby-on-rails mongodb mongoid has-scope

我有两个型号ContentContentType.在内容模型中,我可以这样做:

def get_all_content_except_poking_message
  Content.all.where(:name.ne => "no forking, just poking")
end
Run Code Online (Sandbox Code Playgroud)

现在,我正在尝试在ContentType上应用范围.在内容模型中再次:

# Associations
belongs_to :content_type

def get_all_content_except_certain_content_type(content_type)
  Content.all.where(:content_type.name.ne => content_type)
end
Run Code Online (Sandbox Code Playgroud)

但错误表明它在关联字段上应用范围的语法错误.

在模型中的关联字段上应用范围的正确方法是什么?

我也在使用has_scope gem.我也可以在控制器中应用相同的过滤器吗?就像是:

@contents = apply_scopes(
  if params[:type]
    @content_type = ContentType.find_by_slug(params[:type])
    @content_type.contents.all
  else
    Content.all.where (:content_type.name.ne => "blogs")
  end
)
Run Code Online (Sandbox Code Playgroud)

更新

为了澄清,这里是irb输出:

irb(main):020:0> ContentType.all(:name=>"blogs").count 
=> 1

irb(main):023:0> Content.last.content_type.name 
=> "blogs" 

irb(main):024:0> Content.all.where(:content_type => {:name => {'$ne' => "blogs"}}).count
=> 0 

irb(main):026:0> Content.all.count
=> 4
Run Code Online (Sandbox Code Playgroud)

Gar*_*ami 5

快速回答是MongoDB服务器查询仅在单个集合上运行.没有跨越集合的连接.您正在查询内容集合,但指定content_types集合中的字段.

您可以使用嵌入将两个模型放入一个集合中,然后您的查询可以对嵌入的文档(子)字段起作用.

如果你愿意,我可以提供更多的细节,但希望这会让你超越目前的惊讶.

无需嵌入的要求的附录:

重要说明:从多个集合访问数据将需要多个查询,每个集合至少一个查询.

以下示例基于我可以从您的帖子中提取的内容,并根据您的方法进行修改."不等于"查询利用了关联实现的知识,这只是现在的快速解决方案,但链接结构在检查时非常明显.另请注意,对MongoDB的实际Moped查询显示在相应的Rails日志中.

我不熟悉plataformatec/has_scope的细节.Mongoid有自己的范围你应该调查,我愿意帮助你到达那里.

应用程序/模型/ content_type.rb

class ContentType
  include Mongoid::Document
  field :name, type: String
  has_many :contents
end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ content.rb

class Content
  include Mongoid::Document
  field :name, type: String
  belongs_to :content_type

  def self.get_all_content_except_poking_message
    Content.where(:name.ne => "no forking, just poking")
  end

  def self.get_all_content_except_certain_content_type(content_type_name) # 2 queries - one each for ContentType and Content
    content_type = ContentType.where(:name => content_type_name).first
    Content.where(:content_type_id.ne => content_type.id)
  end
end
Run Code Online (Sandbox Code Playgroud)

测试/单元/ content_test.rb

要求'test_helper'

class ContentTest < ActiveSupport::TestCase
  def setup
    Content.delete_all
    ContentType.delete_all
  end

  test "not equal blogs" do
    blogs = ContentType.create(:name => "blogs")
    tweets = ContentType.create(:name => "tweets")
    blogs.contents << Content.create(:name => "no forking, just poking")
    tweets.contents << Content.create(:name => "Kilroy was here")
    assert_equal 2, ContentType.count
    assert_equal 2, Content.count
    puts "all content_types: #{ContentType.all.to_a.inspect}"
    puts "all contents: #{Content.all.to_a.inspect}"
    puts "get_all_content_except_poking_message: #{Content.get_all_content_except_poking_message.to_a.inspect}"
    puts "get_all_content_except_certain_content_type(\"blogs\"): #{Content.get_all_content_except_certain_content_type("blogs").to_a.inspect}"
  end
end
Run Code Online (Sandbox Code Playgroud)

耙试验

Run options: 

# Running tests:

[1/1] ContentTest#test_not_equal_blogsall content_types: [#<ContentType _id: 51ded9d47f11ba4ec1000001, name: "blogs">, #<ContentType _id: 51ded9d47f11ba4ec1000002, name: "tweets">]
all contents: [#<Content _id: 51ded9d47f11ba4ec1000003, name: "no forking, just poking", content_type_id: "51ded9d47f11ba4ec1000001">, #<Content _id: 51ded9d47f11ba4ec1000004, name: "Kilroy was here", content_type_id: "51ded9d47f11ba4ec1000002">]
get_all_content_except_poking_message: [#<Content _id: 51ded9d47f11ba4ec1000004, name: "Kilroy was here", content_type_id: "51ded9d47f11ba4ec1000002">]
get_all_content_except_certain_content_type("blogs"): [#<Content _id: 51ded9d47f11ba4ec1000004, name: "Kilroy was here", content_type_id: "51ded9d47f11ba4ec1000002">]
Finished tests in 0.046370s, 21.5657 tests/s, 43.1313 assertions/s.
1 tests, 2 assertions, 0 failures, 0 errors, 0 skips
Run Code Online (Sandbox Code Playgroud)

对于这个简单的情况,您可以通过偏离严格的关系规范化来"简化",例如,只需将"content_type_name"字段添加到Content,其字符串值如"blogs".

但要真正利用MongoDB,你应该毫不犹豫地嵌入.

希望这会有所帮助.