简体   繁体   English

查找具有许多关系和其他参数的记录 Ruby on Rails MySQL

[英]Finding a record with has many relationship and other params Ruby on Rails MySQL

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)所以例子是三个Owners ,Frank、Mary、Hillary,他们住在一起并拥有三只pets (doggy、kitty 和 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 pet3 and that she is female.我的目标是返回所有者 1,因为我知道她拥有 pet3 并且她是女性。 I thought I could do something like this:我以为我可以做这样的事情:

found_pet = Pet.find_by(animal: "fish") # will correctly only return the pet3 record
owner = Owner.where(hair_color: "blue").includes(pet: found_pet)

But I keep getting an error (Object doesn't support #inspect) when trying to find owner.但是在尝试查找所有者时,我不断收到错误(Object doesn't support #inspect)

Is this maybe possible with using .join ?使用.join可能吗?

Rails version 6.0.4.7 Rails 版本 6.0.4.7

Ruby version ruby 3.1.1p18 Ruby 版本 ruby​​ 3.1.1p18

UPDATE (Answered in Comments)更新(在评论中回答)

Christos-Angelos Vasilopoulos had a good response for my original question but I had a follow up. Christos-Angelos Vasilopoulos 对我最初的问题做出了很好的回应,但我进行了跟进。

So what if I want to find where the owner own two pets:那么如果我想找到主人拥有两只宠物的地方怎么办:

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)

The best approach would be to use the association.最好的方法是使用关联。 Executing pet_record.owners returns all the pet owners.执行pet_record.owners返回所有宠物主人。 Then you need a query to return the right results.然后你需要一个查询来返回正确的结果。

Use find_by to get the first matching record for your query使用find_by获取查询的第一条匹配记录

found_pet.owners.find_by(hair_color: "blue")

Use where to get all the matching records for your query使用where获取查询的所有匹配记录

found_pet.owners.where(hair_color: "blue")

PS: In your question your state that you need to return Owner1 as it is female, then I would suggest doing the following. PS:在您的问题中,您需要将 Owner1 作为女性返回,那么我建议您执行以下操作。

found_pet.owners.find_by(gender: "female")
found_pet.owners.where(gender: "female")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mysql在一个有多种关系的情况下检索相同的记录 - mysql retrieving the same record in a has many relationship Rails Active Record,从has_many:through关系中获取相关记录,并且where子句通过 - Rails Active Record, get related record from has_many :through relationship with where clause on through record 一对多关系红宝石 - one to many relationship ruby on rails MySQL / Ruby on Rails-如何在:has_many情况下“求和” - MySQL / Ruby on Rails - How to “SUM” in a :has_many case MySQL递归查询,用于查找多对多关系的所有父级 - MySQL recursive query for finding all parents of a many-to-many relationship 通过与 MySQL 的多对多关系查找未在选举中投票的选民 - Finding voters that did not vote in an election through a many to many relationship with MySQL MySQL中有很多关系的方式是什么? - What is the way for a has many relationship in MySQL? 确定MySQL多对多关系中的记录缺失 - Determining record absence in MySQL many-to-many relationship Rails / Active Record has_many通过关联 - 获取记录 - Rails / Active Record has_many through association - fetching a record MySQL - 如何插入具有多对多关系的表中 - MySQL - How to insert into table that has many-to-many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM