Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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 从rails4中的ActiveRecord_关系集合中删除/跳过几个对象_Ruby On Rails_Ruby_Ruby On Rails 4_Activerecord_Rails Activerecord - Fatal编程技术网

Ruby on rails 从rails4中的ActiveRecord_关系集合中删除/跳过几个对象

Ruby on rails 从rails4中的ActiveRecord_关系集合中删除/跳过几个对象,ruby-on-rails,ruby,ruby-on-rails-4,activerecord,rails-activerecord,Ruby On Rails,Ruby,Ruby On Rails 4,Activerecord,Rails Activerecord,如果使用select!,拒绝如果在应收款上,则删除_ @projects=Project.all(有3条状态列值为“failed”的记录) 但是@projects.count=>仍然给出3的结果 意味着如果我们试图对仍然使用旧集合(3条记录)的@projects执行任何查询 有什么建议吗?为什么它使用旧集合,因为它已经通过拒绝操作对其进行了筛选。不希望它使用旧的AR集合 使用ARwhere,我们可以实现它,但在我的情况下,仅使用where无法实现我的结果,需要对AR集合进行一些操作,它应该只返回

如果使用select!,拒绝如果在应收款上,则删除_

@projects=Project.all
(有3条状态列值为“failed”的记录)

但是
@projects.count
=>仍然给出3的结果

意味着如果我们试图对仍然使用旧集合(3条记录)的@projects执行任何查询

有什么建议吗?为什么它使用旧集合,因为它已经通过拒绝操作对其进行了筛选。不希望它使用旧的AR集合


使用AR
where
,我们可以实现它,但在我的情况下,仅使用
where
无法实现我的结果,需要对AR集合进行一些操作,它应该只返回AR而不是数组

您最好使用:

@projects.class
将返回一个
ActiveRecord::Relation
对象,该对象不实现
拒绝方法,因此您不能在尝试时“修改”它。无论如何,这样做是没有意义的

编辑:(自OP编辑后) 我相信你的东西可以通过使用查询接口来实现,但是既然你不能提供具体的例子,这里有一个非有效的方法,但是为什么你会关心效率,对吗

project_ids = @projects.reject { |p| p.status == "failed" }.map(&:id)
new_at_collection = Product.where(id: project_ids)

是的,我同意。但仅通过
where
无法实现我的预期结果,并且需要对AR集合执行操作,它应该返回AR集合,而不是数组。@Rajuakula你问了-我回答。问一个新问题,说明具体的问题,这样你就可以得到具体的答案。@Rajuakula另外,我注意到你从不接受问题的答案-我建议你开始接受正确的答案,如果这些答案对你有用-这样你就让社区知道你的问题已经解决了。这并不能解决我的问题<代码>@projects.reject!{| p | p.status='failed'}
还将以
ActiveRecord::Relation
的形式返回该类。如果能解决我的问题,我当然会接受正确的答案。谢谢
@project.where.not(status: :failed)
project_ids = @projects.reject { |p| p.status == "failed" }.map(&:id)
new_at_collection = Product.where(id: project_ids)