[英]Rails: joins with select
I have two models:- Customer and Account. 我有两个模型:-客户和帐户。
Customer has_one account and account belongs_to customer I want to join the tables and fetch only some fields. 客户has_one帐户和客户归属帐户我想加入表并仅获取某些字段。 My code:-
我的代码:-
Customer.joins(:account).select("customers.id, customers.name, accounts.opening_balance")
It gives me Customer::ActiveRecord_Relation in result like this:- 它给了我Customer :: ActiveRecord_Relation这样的结果:-
[#<Customer:0x00000005be0870 id: 1774, name: "James TEA">,
#<Customer:0x00000005be0730 id: 1777, name: "Joseph STORE">,
#<Customer:0x00000005be0578 id: 1835, name: "John CONFECTIONARY">,
#<Customer:0x00000005be03e8 id: 1836, name: "Jerry PAN SHOP">]
No matter what I do, I am not able to get fields from right table(account in this case). 无论我做什么,我都无法从正确的表(在这种情况下为帐户)获取字段。 Any help?
有什么帮助吗?
Try: 尝试:
customers = Customer.joins(:account).select("customers.id, customers.name, accounts.opening_balance as opening_balance")
customers.first.opening_balance
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.