Ruby on rails ActiveRecord-将Include与Select一起使用

Ruby on rails ActiveRecord-将Include与Select一起使用,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,客户端.rb class Client < ApplicationRecord belongs_to :address end class Address < ApplicationRecord has_one :state has_one :country end 就像我使用了为客户机选择一样,我如何将其用于地址 编辑 如果我使用@client.as_json(include:{address:{except:[:created_at,:update

客户端.rb

class Client < ApplicationRecord
    belongs_to   :address
end
class Address < ApplicationRecord
    has_one :state
    has_one :country
end
就像我使用了
客户机选择
一样,我如何将其用于
地址

编辑

如果我使用
@client.as_json(include:{address:{except:[:created_at,:updated_at]})
生成的哈希没有
created_at


但是当查询运行时,它会在
地址
上执行
选择*
。有没有办法使查询本身只选择所需的列。

您可以使用


您可以使用
only
选项从表中选择特定列

render json: underscore_to_camel(@client.as_json(include: {address: { only: [:address_column1, :address_column2, ...]}}))
列名
替换为
地址列1
地址列2


希望这有帮助

这确实有效。但它仍然从数据库中获取所有列。有没有办法在数据库查询级别限制列?像
select
@mridula从哪个表中选择所有列?@mridula您能在这里发布呈现的
json
吗?对不起,我现在不能发布json。我可以稍后再发。
地址表的所有列。从rails日志中:
客户端加载(1.2ms)选择id、地址id、名称、默认语言、主电话、联系人姓名、联系人电子邮件、联系人电话、网站、来自“客户”订单的备注。“id”ASC限制$1[“限制”,1]
地址加载(19.9ms)从“地址”中选择“地址”。*“id”=$1限额$2[[“id”,2],“限额”,1]]
render json: underscore_to_camel(@client.as_json(include: {address: { except: [:created_at, :updated_at]}}))
render json: underscore_to_camel(@client.as_json(include: {address: { only: [:address_column1, :address_column2, ...]}}))