find_by through collection proxy

This commit is contained in:
Dylan Knutson
2025-07-17 21:52:59 +00:00
parent d3d459da95
commit d83d6833ad
3 changed files with 38 additions and 6 deletions

View File

@@ -305,6 +305,12 @@ RSpec.describe HasAuxTable do
found_car = Car.find_by(fuel_type: "diesel")
expect(found_car).to be_nil
end
it "works with find_by!" do
expect { Car.find_by!(fuel_type: "diesel") }.to raise_error(
ActiveRecord::RecordNotFound
)
end
end
describe "where method with automatic joins" do
@@ -722,6 +728,18 @@ RSpec.describe HasAuxTable do
driver = @car.drivers.create!(name: "John Doe")
expect { driver.destroy }.to change { @car.reload.drivers.count }.by(-1)
end
it "can be queried through the association" do
driver = @car.drivers.create!(name: "John Doe", license_number: 123_456)
expect(@car.drivers.where(name: "John Doe")).to eq([driver])
drivers = @car.drivers
d = drivers.find_by!(license_number: 123_456)
expect(d.id).to eq(driver.id)
d = drivers.find_by(license_number: 123_456)
expect(d.id).to eq(driver.id)
end
end
describe "#reload" do