[ad_1]
I have two tables, pets and owners
class Owner < ApiModel
has_and_belongs_to_many :pets
and
class Pets < ApiModel
has_and_belongs_to_many :owners
So the example is three Owners
, Frank, Mary, Hillary, who live together and own three pets
(doggy, kitty, and fishy)
Owner1 = {name: "Mary", gender: "female", hair_color: "blue", pets: [pet1, pet2, pet3]}
Owner2 = {name: "Hilary", gender: "female", hair_color: "green", pets: [pet1, pet2]}
Owner3 = {name: "Frank", gender: "male", hair_color: "red", pets: [pet3]}
pet1 = {name: "doggy", gender: "female", animal: "dog"}
pet2 = {name: "kitty", gender: "male", animal: "cat"}
pet3 = {name: "fishy", gender: "male", animal: "fish"}
My goal is to return Owner 1 given that I know she owns pet1 and pet2 and that she is female.
I thought I could do something like this:
found_pet1 = Pet.find_by(animal: "dog")
found_pet2 = Pet.find_by(animal: "cat")
owner = Owner.where(hair_color: "blue").includes(pet: found_pet1).includes(pet: found_pet2)
But I keep getting an error (Object doesn't support #inspect)
when trying to find owner.
Is this maybe possible with using .join
?
Rails version 6.0.4.7
Ruby version ruby 3.1.1p18
[ad_2]