Ruby-on-rails 在 Rails 中渲染 JSON 时包含关联模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17730121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Include associated model when rendering JSON in Rails
提问by Hommer Smith
Right now I have this line:
现在我有这条线:
render json: @programs, :except => [:created_at, :updated_at]
However, since a Program belongs_to a Company I would like to show the Company name instead of the Company Id.
但是,由于程序属于公司,我想显示公司名称而不是公司 ID。
How can I include the company name when rendering Programs?
渲染程序时如何包含公司名称?
回答by Nobita
Something like this should work:
这样的事情应该工作:
render :json => @programs, :include => {:insurer => {:only => :name}}, :except => [:created_at, :updated_at]
回答by Sean M
i was getting the same "can't clone Symbol file" error while rendering json with includes from a controller method. avoided it like so:
在使用控制器方法中的包含渲染 json 时,我遇到了相同的“无法克隆符号文件”错误。像这样避免它:
render :json => @list.to_json( :include => [:tasks] )
回答by Abram
You can also do this at the model level.
您也可以在模型级别执行此操作。
program.rb
程序.rb
def as_json(options={})
super(:except => [:created_at, :updated_at]
:include => {
:company => {:only => [:name]}
}
)
end
end
Now in your controller:
现在在您的控制器中:
render json: @programs
回答by Arnlen
Consider using jbuilderto include nested models in a maintainable way:
考虑使用jbuilder以可维护的方式包含嵌套模型:
# /views/shops/index.json.jbuilder
json.shops @shops do |shop|
# shop attributes to json
json.id shop.id
json.address shop.address
# Nested products
json.products shop.products do |product|
json.name product.name
json.price product.price
end
end
回答by IliasT
Try this. Ref
尝试这个。参考
#`includes` caches all the companies for each program (eager loading)
programs = Program.includes(:company)
#`.as_json` creates a hash containing all programs
#`include` adds a key `company` to each program
#and sets the value as an array of the program's companies
#Note: you can exclude certain fields with `only` or `except`
render json: programs.as_json(include: :company, only: [:name])
Also, no need to make @programsan instance variable, as I'm assuming we are not passing it to a view.
另外,不需要创建@programs实例变量,因为我假设我们没有将它传递给视图。
回答by user3716986
#includes is used to avoid n+1 query.
# http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations
Here is an example for the above example.Lets say you have posts and each post has many comments to it.
@posts = Post.where('id IN [1,2,3,4]').includes(:comments)
respond_to do |format|
format.json {render json: @posts.to_json(:include => [:comments]) }
end
#output data
[
{id:1,name:"post1",comments:{user_id:1,message:"nice"}}
{id:2,name:"post2",comments:{user_id:2,message:"okok"}}
{id:3,name:"post1",comments:{user_id:12,message:"great"}}
{id:4,name:"post1",comments:{user_id:45,message:"good enough"}}
]

