简体   繁体   中英

Active Record: How to find a Parent based on an that there is no association with a given attribute

I have the following Database structure:

Parent
 -> child 1
 -> child 2
 ->.. 

Now I want to find all the parents who don't have a child with a specific attribute set to true.

Example:

Should not be found: 
Parent: 
 -> child 1 (selected: true)
 -> child 2 (selected: false)

Should be found
Parent: 
 -> child 1 (selected: nil)
 -> child 2 (selected: false)

Currently I have the following:

Project.all - Project.includes(:project_images).where(project_images: { selected: true })

This works fine but it seems to be somewhat inefficient. I believe there should be an easier and faster way.

You can just use where.not to solve it.

see https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods/WhereChain.html#method-i-not

Project.joins(:project_images).where.not(project_images: { selected: true })
    parents = Parent.joins(:children).where('children.selected = FALSE or children.selected is NULL')

This query selects rows from parents children inner join table with selected attribute not set to true.

If you want parent objects without even an asociated child you have to left outer join children.

Parent.joins('left join children on parents.id = childrenparent_id')..where('children.selected = FALSE or children.selected is NULL')

Project.joins(:project_images).where(“ project_images.selected NOT IN(TRUE)”);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM