I have join between two tables. Instruments is the parent table and Statuses are the child table. I am looking only to retrieve those instruments having the inst_status = 'Active' in the child Statuses table.
i = Instrument.joins(:statuses).where("statuses.inst_status='Active'")
The resulted query generated is:
SELECT "instruments".* FROM "instruments" INNER JOIN "statuses" ON "statuses"."instrument_id" = "instruments"."id" WHERE (statuses.inst_status='Active')
Which is OK it is returning only 55 instruments which are Active. However, how can I get the fields from the Statuses (child) table in the result set along with the fields from the (parent) Instruments table?
Well, You have to use select
method for this :
Instrument.joins(:statuses)
.where("statuses.inst_status='Active'")
.select("instruments.*, statuses.*")
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.