如果我提交
Role.select("roles.character, actors.lname AS actors_lname").joins(:actor)
它返回:
Role Load (0.0ms) SELECT roles.character, actors.lname AS actors_lname
FROM "roles" INNER JOIN "actors" ON "actors"."id" = "roles"."actor_id"
#<ActiveRecord::Relation [#<Role id: nil, character: "Ellis Boyd 'Red' Redding">,
#<Role id: nil, character: "Andy Dufresne">, #<Role id: nil, character: "Warden Norton">]>
为什么没有显示actors.lname列?
使用select
。
Order.select("orders.id, customers.name").joins(:customers)
如果为它们取别名,则可以获取它们
orders = Order.select("orders.id, customers.name AS customer_name").joins(:customers)
# you must call the method implicitly, or use .attributes
orders.first.customer_name
请注意,的值customer_name
不会显示在检查记录中。因此下面的代码
orders.first
在IRB中不会打印出该属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句