Ruby on rails &引用;“不相等”;使用Mongoid在rails中命名范围

Ruby on rails &引用;“不相等”;使用Mongoid在rails中命名范围,ruby-on-rails,mongodb,model-view-controller,mongoid,has-scope,Ruby On Rails,Mongodb,Model View Controller,Mongoid,Has Scope,我有两种型号Content和ContentType。在内容模型中,我可以做到: 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_exce

我有两种型号
Content
ContentType
。在内容模型中,我可以做到:

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
但这个错误表明,将作用域应用于关联的字段是错误的语法

在模型中,对关联字段应用作用域的正确方法是什么

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

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

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

如果你愿意的话,我可以提供更多的细节,但希望这能让你克服目前的震惊

未嵌入以下内容的补遗:

重要提示:访问多个集合中的数据将需要多个查询,每个集合至少需要一个查询

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

我不熟悉plataformatec/has_范围的细节。 Mongoid有自己的范围,你应该调查,我愿意在你到达那里时提供帮助

app/models/content\u 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
测试/单元/内容\u test.rb

需要“测试助手”

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
classcontenttest“blogs”)
tweets=ContentType.create(:name=>“tweets”)
blogs.contents“没有分叉,只是戳一下”)
tweets.contents“Kilroy在这里”)
断言_等于2,ContentType.count
断言_等于2,Content.count
将“所有内容类型:{ContentType.all.to_a.inspect}”
将“所有内容:{Content.all.to_a.inspect}”
将“get_all_content(获取所有内容)_About_poking_message:#{content.get_all_content(获取所有内容)_About_poking_message.to_a.inspect}”
将“获取除特定内容类型(“博客”)之外的所有内容:{content.get除特定内容类型(“博客”)之外的所有内容。发送到\u a.inspect}”
结束
结束
耙试验

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
运行选项:
#运行测试:
[1/1]ContentTest#test#not(u equal)blog所有内容类型:[#,#]
所有内容:[#,#]
获取除戳戳信息外的所有内容:[
获取除特定内容类型(“博客”)之外的所有内容:[
以0.046370秒、21.5657次测试/秒、43.1313次断言/秒的速度完成测试。
1次测试,2次断言,0次失败,0次错误,0次跳过
对于这种简单的情况,您可以通过脱离严格的关系规范化来“简化”,例如,只需将“content\u type\u name”字段添加到具有字符串值(如“blogs”)的内容中

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


希望这能有所帮助。

Gary,谢谢你的回答。如果嵌入式关联不是一个选项,是否有其他方法来模拟关联?如果您能提供一些针对我的场景的代码作为示例,我将不胜感激。:)Annie,上面答案中的示例针对您的场景,使用引用/链接而不是嵌入。请检查内容代码::获取所有内容,但某些内容类型除外,该类型通过必要的两阶段获取准确回答您的问题。