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 4 ActiveRecord。其中一个数组中的任何元素都在另一个数组中_Ruby On Rails_Ruby On Rails 4_Activerecord - Fatal编程技术网

Ruby on rails Rails 4 ActiveRecord。其中一个数组中的任何元素都在另一个数组中

Ruby on rails Rails 4 ActiveRecord。其中一个数组中的任何元素都在另一个数组中,ruby-on-rails,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby On Rails 4,Activerecord,我有一个ID列表,如下所示: feed_ids = [1,2,3,4,5] # will be filled with random ids parent_ids = [20,14,1] 我有一个Post模型,它有一个属性parent_id,可能看起来像这样: feed_ids = [1,2,3,4,5] # will be filled with random ids parent_ids = [20,14,1] 我想检索父\u id中的元素与提要\u id中的元素匹配的所有记录 我试

我有一个ID列表,如下所示:

feed_ids = [1,2,3,4,5] # will be filled with random ids
parent_ids = [20,14,1]
我有一个Post模型,它有一个属性parent_id,可能看起来像这样:

feed_ids = [1,2,3,4,5] # will be filled with random ids
parent_ids = [20,14,1]
我想检索父\u id中的元素与提要\u id中的元素匹配的所有记录

我试过这个,但不起作用:

nodes = Post.where(parent_ids: feed_ids)

它没有给我一个错误,但也没有返回任何记录

find方法可以接受数组。这里可以使用数组交集

Post.find(parent_ids & feed_ids)
免责声明

我没有安装导轨,所以我必须凭直觉去做


此外,如果您有一个大型数据集,这可能不是最有效的解决方案。但由于记录相对较少,这应该没问题

你确定数据库中有帖子吗?你的语法是正确的,我在我的应用程序上试过。
Post.find(parent\u id&feed\u id)
@richardhilton的评论应该有用!可能会觉得有点不妥,但对于您的特定用例,这就是解决方案。不知道你为什么需要这个。对一个问题提出一些背景/观点可能有助于您更好地了解解决方案。我已取消删除我的帖子。我以前不确定它是否有效