gpt4 book ai didi

ruby-on-rails - Rails 预先加载与选定属性的关联

转载 作者:行者123 更新时间:2023-12-04 15:13:00 25 4
gpt4 key购买 nike

谁能告诉我是否可以急切加载关联但只返回特定属性?

我想用他们的帐户检索一些订单,但只需要帐户名称。
Order.select([:id, :account_id]).includes(:account).limit(2)

最佳答案

我认为 jvnill 的评论说明了一切:

I'm not sure that is possible. You can however add the account_name to the returned order records.


orders = Order.joins(:account).select('orders.id, orders.account_id, accounts.name AS account_name')

then just use account_name like orders.first.account_name

关于ruby-on-rails - Rails 预先加载与选定属性的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15358099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com