【发布时间】:2017-04-20 16:13:12
【问题描述】:
client.rb
class Client < ApplicationRecord
belongs_to :address
end
address.rb
class Address < ApplicationRecord
has_one :state
has_one :country
end
来自Client#show 我想返回一个包含客户端详细信息及其地址的 JSON。但是,address 中有一些我不想返回的字段(例如 created_at、updated_at)。
我写这个来渲染 JSON -
def show
@client = Client.select("id, address_id").first
render json: underscore_to_camel(@client.as_json(:include => :address))
end
就像我将select 用于Client 我如何将它用于Address?
编辑
如果我使用@client.as_json(include: {address: { except: [:created_at, :updated_at]}}),则生成的哈希没有created_at。
但是当查询运行时,它会在addresses 上执行select *。有没有办法做到这一点,以便查询本身只选择所需的列。
【问题讨论】:
标签: ruby-on-rails activerecord