繁体   English   中英

Rails ActiveRecord where子句不起作用

[英]Rails ActiveRecord where clause not working

我正在将Mongoid与Rails结合使用。 我有两个模型: sprintbuild 一个build有很多sprints

sprint.rb:

class Sprint
  include Mongoid::Document
  belongs_to :build
end

build.rb:

class Build
  include Mongoid::Document
  has_many :sprints
end

sprints ,我想所有的builds包含当前冲刺,所以我做的:

def builds
    Build.where("sprint" => self)
end

在我show.html.erbsprints ,我做的:

<%= render @sprint.builds %>

但是,我得到一个例外:

Sprint的undefined method bson_dump ':0x007fd1acb68200

如果我将builds方法更改为:

  def builds
    list = []
    Build.all.each do |build|
      if build.sprints.include? self
        list.push(build)
      end
    end
    list
  end

然后一切正常,我得到了预期的结果。 可能是什么问题?

编辑:我还应该补充一点,并不是所有的构建都有sprint,这意味着某些构建的sprint值可能为nil。

ActiveRecord where方法用于构造SQL查询。 它不知道如何处理Sprint对象作为哈希中的值。 您需要寻找一些Sprint属性,例如id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM