Возникли проблемы с использованием where.не в rails

#ruby-on-rails

#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:

Вы можете указать хэш внутри where , чтобы указать имена таблиц в качестве ключей и имена столбцов на втором уровне:

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