[英]rails render json with query
I would like to include a username (a column in the user table) for each review in the following json response 我想在以下json响应中为每个评论添加用户名(用户表中的列)
render json: Doctor.all, :include => :reviews
What's the best way to accomplish this? 做到这一点的最佳方法是什么? Should I include the username in the reviews table or make a query before returning the json?
在返回json之前,我应该在评论表中包含用户名还是进行查询?
The reviews table schema is includes content
, user_id
and doctor_id
评论表架构包括
content
, user_id
和doctor_id
If it helps, I've included the relations below 如果有帮助,我在下面列出了以下关系
class Doctor < ActiveRecord::Base
has_many :reviews
has_many :users, through: :reviews
end
class Review <ActiveRecord::Base
belongs_to :user
belongs_to :doctor
end
class User < ActiveRecord::Base
has_many :reviews
has_many :doctors, through: :reviews
end
Thanks @ali in the comments. 感谢@ali的评论。
I used :include => {:reviews => {:include => {:user => { :only => :username }}}}
我用了
:include => {:reviews => {:include => {:user => { :only => :username }}}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.