I am making a web application with rails.
I encountered an N + 1 problem and started using includes.
I asked a question without knowing the following method.
- There are two models, Group and User
- User belongs to any one group (associated)
- Read both tables as Group.includes (: user)
- At this time, the child table user wants to read only males
- I want to read both parent tables
If i search, you will see that the child table is used to narrow down the parent table.
I couldn't find a way to narrow down only the child table without narrowing down the parent table.
(Did you find it?)
If i execute the following, only GroupA can be extracted, and the parent table Group wants to output both A and B.
I tried to try
Rails ## controller @groups = Group.includes (: users) .where (gender: male) .references (: users) ## view @ groups.each do | group | group.name end
scope etc. but it didn't work and I would appreciate your advice.Supplemental information (FW/tool version etc.)
The Rails version is 5.2.2.
DB is developed with mySQL, cloud9.
Answer # 1
Only child records specified by specifying a join condition can be joined.
Group.eager_load (: users) .joins ("AND users.gender = male")
I think it would have been better to use
eager_loadwhen it is always combined.
- ruby - i want to search records of rails child model using columns of parent model
- ruby - [rails] i want to add multiple records for each date and reflect them
- ruby on rails 5 - child model record not created
- in ruby on rails, i want to limit it to information other than foreign key constraint in references
- ruby - i want to collect the same records using rails group and get the value
- ruby - i got stuck in chapter 5 of the rails tutorial i want to apply it because applicationhtmlerb is not applied
- ruby on rails - i can't get aws environment variables in rails
- ruby on rails - i want to api communication between containers created with docker-compose
- ruby on rails data is not saved in db? ??
- ruby - [rails] posts added to favorites are not saved
- ruby on rails - i want to make a flea market app with rails and stripe, but i can't have a system to send money to the seller
- ruby - [rails] data is not saved due to activerecord :: notnullviolation error
- ruby on rails - [rails] form_with submit button does not respond
- ruby on rails - the transition destination of link_to does not work
- ruby on rails - i can't implement the like function
- ruby on rails 6 - @ commentsave cannot be done
- ruby - if i accidentally do rails db: drop, the database disappears and i can't deploy it can i get it back?
- ruby - rails can't receive the value passed from the form
- ruby - [rails] i want to use fields_for with form_with, but an error occurs and the page cannot be displayed
- ruby on rails - i want to pass the id of the choicekadai table
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- python 3x - typeerror: 'method' object is not subscriptable
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- xcode - pod install [!] no `podfile 'found in the project directory
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- android studio - unresolved reference comes out in kotlin