在rails中使用where.not时遇到问题

时间:2016-10-22 04:31:55

标签: ruby-on-rails

我正在尝试使用where.not来替换以下内容:

if @friend_matches.count > 0
  @court_matches = Match.available_on_courts.where('matches.id NOT IN (?)', @friend_matches.pluck(:id)).to_a
else
  @court_matches = Match.available_on_courts
end

使用

    @court_matches = Match.available_on_courts.where.not(matches.id: @friend_matches.pluck(:id)).to_a

但是我收到以下错误。

SyntaxError: /Users/sripaladugu/Coding/matchpoint_rails/app/mailers/match_mailer.rb:8: syntax error, unexpected ':'
...on_courts.where.not(matches.id: @friend_matches.pluck(:id))....
...                               ^
/Users/sripaladugu/Coding/matchpoint_rails/app/mailers/match_mailer.rb:8: syntax error, unexpected ')', expecting keyword_end
...id: @friend_matches.pluck(:id)).to_a

1 个答案:

答案 0 :(得分:2)

您可以在where内提供散列,以将表名指定为第二级中的键和列名:

@court_matches = Match.available_on_courts
                      .where.not(matches: { id: @friend_matches.pluck(:id) })
                      .to_a