【问题标题】:"Not equal" named scope in rails with Mongoid在带有 Mongoid 的 Rails 中“不等于”命名范围
【发布时间】:2013-05-14 01:48:04
【问题描述】:

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

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

现在,我正在尝试对 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

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

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

我也在使用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
)

更新

为了澄清,这里是 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

【问题讨论】:

    标签: ruby-on-rails mongodb model-view-controller mongoid has-scope


    【解决方案1】:

    快速回答是 MongoDB 服务器查询仅对单个集合进行操作。没有跨集合的连接。 您正在查询内容集合,但指定了 content_types 集合中的字段。

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

    如果您愿意,我可以提供更多细节,但希望这会让您摆脱当前的惊讶。

    按要求添加不嵌入的附录:

    重要提示:访问来自多个集合的数据将需要多次查询,每个集合至少有一个查询。

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

    我不熟悉 plataformatec/has_scope 的细节。 Mongoid 有自己的作用域,你应该调查一下,当你到达那里时,我愿意提供帮助。

    app/models/content_type.rb

    class ContentType
      include Mongoid::Document
      field :name, type: String
      has_many :contents
    end
    

    app/models/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
    

    test/unit/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 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
    

    对于这种简单的情况,您可以通过脱离严格的关系规范化来“简化”,例如,只需将“content_type_name”字段添加到具有字符串值(如“blogs”)的内容。

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

    希望这会有所帮助。

    【讨论】:

    • 加里,感谢您的回答。如果嵌入式关联不是一个选项,是否有另一种模拟关联的方法?如果您提供一些针对我的场景的代码作为示例,我将不胜感激。 :)
    • Annie,上面答案中的示例针对您的场景使用引用/链接而不是嵌入。请检查 Content ::get_all_content_except_certain_content_type 的代码,该代码通过必要的两阶段提取准确回答了您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 2015-02-17
    相关资源
    最近更新 更多