Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.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 activerecord多次连接同一个表_Ruby On Rails_Activerecord_Parameters_Active Record Query - Fatal编程技术网

Ruby on rails activerecord多次连接同一个表

Ruby on rails activerecord多次连接同一个表,ruby-on-rails,activerecord,parameters,active-record-query,Ruby On Rails,Activerecord,Parameters,Active Record Query,我正在做一个像这样的简单搜索(使用8个不同的参数,但我将复制2个用于示例) 当我只有一个参数“on”时,查询工作正常,并返回参数中包含服务的事件 但是如果我有两个参数“开”,即使我的事件有两个服务,它也不再工作了 我已经在控制台中对它进行了测试,我可以看到,如果我对之前已经加入的元素执行.joins(:services).where(services:{name:“big”}),它将不会返回任何结果 我不明白为什么 第一个@events(当一个参数时)返回一个活动记录关系,其中包含几个事件 为什

我正在做一个像这样的简单搜索(使用8个不同的参数,但我将复制2个用于示例)

当我只有一个参数“on”时,查询工作正常,并返回参数中包含服务的事件

但是如果我有两个参数“开”,即使我的事件有两个服务,它也不再工作了

我已经在控制台中对它进行了测试,我可以看到,如果我对之前已经加入的元素执行.joins(:services).where(services:{name:“big”}),它将不会返回任何结果

我不明白为什么

第一个@events(当一个参数时)返回一个活动记录关系,其中包含几个事件

为什么我不能再做一次呢,加入吧

我真的不明白这个查询出了什么问题,也不明白为什么两次连接后它就会变成空的


非常感谢

您使用的代码将转换为:

SELECT  "events".* FROM "events" INNER JOIN "services" ON "services"."event_id" = "events"."id" WHERE "services"."name" = ? AND "services"."name" = ? LIMIT ?  [["name", "big"], ["name", "old"], ["LIMIT", 11]]
这就是它返回零记录的原因

这是我目前能想到的解决方案,不确定它是否理想,但它确实有效,并且已经过测试

# event.rb
class Event < ApplicationRecord
  has_many :services
  has_many :old_services, -> { where(name: 'old') }, class_name: 'Service'
  has_many :big_services, -> { where(name: 'big') }, class_name: 'Service'
end


# service.rb
class Service < ApplicationRecord
  belongs_to :event
end

原因是您的链接
where
将在Sql查询中生成
子句,如
where services.name=“old”和services.name=“big”
,这在您的数据库中不存在。我想您的意图是使用
而不是
,对吗?嗯,我想我确实需要一个AND。如果我只有一个参数,比如说“big”,我希望所有事件都有“big”作为服务。但是一个活动可以有很多服务。因此,如果用户放置两个参数“big”和“old”,我希望所有事件都有“big”和“old”作为服务。。。当我在我的控制台中查看时,它确实存在。如果我确实想要一个和,并且如果我有一个包含这两个服务的事件,它应该可以工作吗?我错过了什么?不,那不行。因为您的两个服务共享相同的名称变量。您将同时执行“name==big”和“name==old”,它们不能同时相等。聪明!谢谢。昨天我找到了另一个解决方案,那就是迭代所有事件,对每个事件执行.pull(:services),并检查循环中的数组是否与参数中的数组相同。如果是,我将此事件推送到一个新数组中。这真的是不公平的(事实上很糟糕,但我想不出还有什么别的)。我认为你的解决方案更好更干净!我会尝试它的魅力,甚至帮助我重构代码并循环使用所有服务参数:)谢谢好的,很高兴听到这个消息。
# event.rb
class Event < ApplicationRecord
  has_many :services
  has_many :old_services, -> { where(name: 'old') }, class_name: 'Service'
  has_many :big_services, -> { where(name: 'big') }, class_name: 'Service'
end


# service.rb
class Service < ApplicationRecord
  belongs_to :event
end
if params[:old] == "on"
  @events = @events.joins(:old_services)
end

if params[:big] == "on"
  @events = @events.joins(:big_services)
end

@events = @events.distinct
# SELECT  DISTINCT "events".* FROM "events" INNER JOIN "services" ON "services"."event_id" = "events"."id" AND "services"."name" = ? INNER JOIN "services" "old_services_events" ON "old_services_events"."event_id" = "events"."id" AND "old_services_events"."name" = ? LIMIT ?  [["name", "big"], ["name", "old"], ["LIMIT", 11]]