请参阅以
ActiveModel::Serializers::JSON#as_json查看可以传递给的选项
render:json。报价:
要包括关联,请使用
:include…二级和更高级别的关联也可以工作:
> user.as_json(:include => { :posts => {> :include => { :comments => {> :only => :body } },> :only => :title } })> # => { "id": 1, "name": "Konata Izumi", "age": 16,> # "created_at": "2006/08/01", "awesome": true,> # "posts": [ { "comments": [ { "body": "1st post!" }, { "body":> "Second!" } ],> # "title": "Welcome to the weblog" },> # { "comments": [ {"body": "Don't think too hard" } ],> # "title": "So I was thinking" } ]> # }不需要像自动那样直接调用
to_json或
as_json直接调用
render :json。



