简体   繁体   English

如何从结果集中的子表中获取字段

[英]How to get fields from the child table in the result set

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. 我只想检索子状态表中具有inst_status ='Active'的那些工具。

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. 可以,它只返回55个处于活动状态的仪器。 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 : 好吧,您必须为此使用select方法:

Instrument.joins(:statuses)
          .where("statuses.inst_status='Active'")
          .select("inst‌​ruments.*, statuses.*")

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

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