简体   繁体   English

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

[英]RubyOnRails4 How do I exclude properties from a has_many relationship when rendering JSON

Is there a way to exclude properties from :jobs like I've done with @databases? 有没有办法像我对@databases一样从:jobs中排除属性? I'm new to ruby and rails so I suspect (read: hope) that my syntax is wrong and this is an easy problem to fix. 我是红宝石和Rails的新手,所以我怀疑(希望:读到)我的语法错误,这很容易解决。

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

如果@databases对象的模型类具有has_many :jobs则可以执行以下操作:

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

Solution

Added => { :except => [:id] } to :jobs 已将=> { :except => [:id] }:jobs

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM