在Rails中添加复杂的查询

时间:2018-09-25 12:27:39

标签: ruby-on-rails postgresql join nested where

在Rails中进行查询时,情况很复杂,模型如下:

  • 路线has_many感兴趣
  • 路线has_many天
  • 天有has_many点

我有2个查询,一个按兴趣搜索路线(标题(不止一个)):

    joins(:interests).where(interests: { title: interest.split(',')}).group('routes.id').having('count(interests.route_id) = ?', interest.split(',').length)

和其他按Point.title(多于一个)搜索路线

    joins(:days => :points).where(days: { points: { place_id: point.split(',')}}).group('routes.id').having('count(routes.id) = ?', point.split(',').length)

但是我的问题是,当我尝试同时添加两者时,我使用了:

    joins(:interests).where(interests: { title: interest.split(',')}).group('routes.id').joins(:days => :points).where(days: { points: { place_id: point.split(',')}}).group('routes.id').having('count(routes.id) = ?', point.split(',').length+interest.split(',').length)

但是不起作用

0 个答案:

没有答案