Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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 5查询返回:ActiveRecord\u关系而不是数据_Ruby On Rails_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails Rails 5查询返回:ActiveRecord\u关系而不是数据

Ruby on rails Rails 5查询返回:ActiveRecord\u关系而不是数据,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,我试图使用活动记录访问数据库中的一些特定数据,而不是实际数据。我敢打赌我错过了一些很容易的东西,但我无法理解。 查询: 使用find_by代替where 其中返回ActiveRecord::Relation和find_by只返回一条记录 如果您的请求将返回一条记录,您可以使用where with first,如下所示: version_one = ScrapedPage.select("html").where(domain: @domain, created_at: params[:versi

我试图使用活动记录访问数据库中的一些特定数据,而不是实际数据。我敢打赌我错过了一些很容易的东西,但我无法理解。 查询:

使用find_by代替where

其中返回ActiveRecord::Relation和find_by只返回一条记录

如果您的请求将返回一条记录,您可以使用where with first,如下所示:

version_one = ScrapedPage.select("html").where(domain: @domain, created_at: params[:version_one]).first
version_one.html
version_one是一个关系对象,您可以访问如下html数据:

version_one = ScrapedPage.select("html").where(domain: @domain, created_at: params[:version_one]).first
version_one.html
您可以通过下面的find_by和where查看查询结果


我用find_by替换了where,即使我使用,也会得到同样的结果。首先。不完全相同,但这个@Padu Show me查询使用。第一个``version\u one=ScrapedPage.selecthtml.wheredomain:@domain,创建于:params[:version\u one]。第一个版本\u two=ScrapedPage.selecthtml.wheredomain:@domain,创建于:params[:version\u two].首先``给你go@Padu我使用find_by和where运行了查询,并将结果输入到答案中。我认为您应该使用inspect方法跟踪控制台以了解更多详细信息。我尝试过像这样从控制台运行它,它可以运行version\u one=ScrapedPage.selecthtml.find\u bydomain:'test1',创建于:'1990-01-01 00:00:01 UTC'=>所以我认为我的参数可能有问题。这是与。通过查找。