Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Rails中查找非孤立记录_Ruby On Rails - Fatal编程技术网

Ruby on rails 在Rails中查找非孤立记录

Ruby on rails 在Rails中查找非孤立记录,ruby-on-rails,Ruby On Rails,问题的后续行动: 我想知道如何获得作为AssociationRelation而不是数组返回的所有非孤立记录。当尝试从rails中减去表的总记录时,6.缺少的记录,结果是正确的,但它是以数组的形式出现的 下面是一个控制台示例: p = ProductResearch.first (Product.all - p.products.where.missing(:keywords)).class => Array 我怎样才能得到这个协会 (在下面@max的帮助下,我找到了一个查询,没有遗漏,它

问题的后续行动:

我想知道如何获得作为AssociationRelation而不是数组返回的所有非孤立记录。当尝试从rails中减去表的总记录时,6.缺少的记录,结果是正确的,但它是以数组的形式出现的

下面是一个控制台示例:

p = ProductResearch.first
(Product.all - p.products.where.missing(:keywords)).class
=> Array
我怎样才能得到这个协会

(在下面@max的帮助下,我找到了一个查询,没有遗漏,它以关联的形式返回预期结果,如下所示:

irb(main):206:0> p.products.includes(:keywords).where.not(keywords: { id: nil }).class
=> Product::ActiveRecord_AssociationRelation
它只返回非孤立项。

给定:

class Post < ApplicationRecord
  has_many :comments
end
如果对文章进行内部联接,则在联接表中只能得到至少有一个匹配项的行:

irb(main):014:0> Comment.joins(:post)
  Comment Load (0.3ms)  SELECT "comments".* FROM "comments" INNER JOIN "posts" ON "posts"."id" = "comments"."post_id" LIMIT ?  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Comment id: 1, post_id: 1, created_at: "2021-05-11 08:59:04", updated_at: "2021-05-11 08:59:04">, #<Comment id: 2, post_id: 1, created_at: "2021-05-11 08:59:04", updated_at: "2021-05-11 08:59:04">, #<Comment id: 3, post_id: 1, created_at: "2021-05-11 08:59:04", updated_at: "2021-05-11 08:59:04">]>

你的意思是
all.count-where.missing.count
?但是你会得到一个ActiveRecord关系实例,而不是一个。这是一个关于如何处理
where.not.missing
?你当前拥有的代码是什么,并且正在返回一个数组?因为从文档中它应该返回一个关系。尝试使用Rails 6.1.3.2,它会返回一个关系这也是一种关系。是的@LamPhan,这一个。非常感谢@max!我发现缺少的指令非常棒,这就是为什么我想知道是否有一种方法可以使用缺少而不是自定义联接。不,我认为你不能使用
。不。缺少
。仅仅使用
。联接(:association\u name)的真正问题是什么
?一点问题都没有,我只是想知道,因为它看起来更光滑。我试过不使用missing,但不起作用。我想我将不得不求助于联接。
。不。missing
如果生成
注释,就会起作用。left_联接(:post)。where.not(posts:{id:nil})
但我不这么认为。我想你可以编写你自己的
不丢失
方法-但即使它更圆滑,它仍然会迫使开发人员查找该方法-而大多数人应该知道
。连接
做什么。是的,我实际上只是尝试了左连接,就像,它不起作用确实返回了整个集合。仍然有问题但是,如果使用第一个联接来处理非孤立的联接,我认为这是因为它从整个表返回联接?不确定,我已经很久没有处理过联接了:D但是p.products.count在我的例子中有58个元素,p.products.joins(:关键字).count返回240个元素:/它应该返回20个。当使用缺少的元素或左连接时,我会返回38个元素。
class CreateComments < ActiveRecord::Migration[6.0]
  def change
    create_table :comments do |t|
      # Referential integrity is for wusses! YOLO!
      t.belongs_to :post, null: true, foreign_key: false
      t.timestamps
    end
  end
end
p1 = Post.create!(title: 'Foo')
3.times { p1.comments.create! }
p2 = Post.create!(title: 'Bar')
3.times { p2.comments.create! }
p2.destroy! # orphans the comments
irb(main):014:0> Comment.joins(:post)
  Comment Load (0.3ms)  SELECT "comments".* FROM "comments" INNER JOIN "posts" ON "posts"."id" = "comments"."post_id" LIMIT ?  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Comment id: 1, post_id: 1, created_at: "2021-05-11 08:59:04", updated_at: "2021-05-11 08:59:04">, #<Comment id: 2, post_id: 1, created_at: "2021-05-11 08:59:04", updated_at: "2021-05-11 08:59:04">, #<Comment id: 3, post_id: 1, created_at: "2021-05-11 08:59:04", updated_at: "2021-05-11 08:59:04">]>
irb(main):016:0> Comment.left_joins(:post).where(posts: { id: nil })
  Comment Load (0.3ms)  SELECT "comments".* FROM "comments" LEFT OUTER JOIN "posts" ON "posts"."id" = "comments"."post_id" WHERE "posts"."id" IS NULL LIMIT ?  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Comment id: 4, post_id: 2, created_at: "2021-05-11 08:59:26", updated_at: "2021-05-11 08:59:26">, #<Comment id: 5, post_id: 2, created_at: "2021-05-11 08:59:26", updated_at: "2021-05-11 08:59:26">, #<Comment id: 6, post_id: 2, created_at: "2021-05-11 08:59:26", updated_at: "2021-05-11 08:59:26">]>
Comment.where.missing(:post)