RubyOnRails4如何在呈现JSON时从has_many关系中排除属性

时间:2014-10-08 18:41:12

标签: ruby-on-rails ruby json ruby-on-rails-4

有没有办法排除属性:像@databases这样的工作?我是ruby和rails的新手,所以我怀疑(读:希望)我的语法错误,这是一个很容易解决的问题。

wants.json { render :json => @databases, :include => [:jobs], :except => [:id, :created_at, :updated_at] }

2 个答案:

答案 0 :(得分:0)

如果你的@databases对象的模型类has_many :jobs那么,你可以这样做:

render :json => @databases.to_json(:include => {:jobs => {:except => [:created_at, updated_at]}}, :except => [:id, :created_at, :updated_at])

答案 1 :(得分:0)

<强>解决方案

=> { :except => [:id] }添加到:jobs

wants.json { render :json => @databases, :include => [:jobs => { :except => [:id] }, :except => [:id, :created_at, :updated_at] }