У вас действительно правильный запрос.Скорее, ваш метод тестирования не работает.
class DoctorLocation < ApplicationRecord
belongs_to :location
def self.with_confidence(c)
DoctorLocation.joins(:location).where(locations: { confidence: c })
end
end
Эта проходящая спецификация подтверждает, что она работает как положено:
require 'rails_helper'
RSpec.describe DoctorLocation, type: :model do
after(:each) { DoctorLocation.destroy_all }
it "includes locations with the correct confidence" do
dl = DoctorLocation.create!(location: Location.create!(confidence: 2))
DoctorLocation.create!(location: Location.create!(confidence: 1))
expect(DoctorLocation.with_confidence(2)).to include dl
expect(DoctorLocation.with_confidence(2).count).to eq 1
end
it "does not include rows without a match in the join table" do
dl = DoctorLocation.create!(location: Location.create!(confidence: 1))
expect(DoctorLocation.with_confidence(2)).to_not include dl
end
end