0
JSON我想包括每个评论用户名(在用户表中的列)在以下JSON响应轨渲染查询
render json: Doctor.all, :include => :reviews
什么是实现这一目标的最佳方式是什么?我应该在评论表中包含用户名还是在返回json之前进行查询?
的审查表架构是包括content
,user_id
和doctor_id
如果有帮助,我已经包括以下
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
试试:'渲染JSON:Doctor.all.to_json(:包括=> {:审查=> {:包括=>:用户=> {:仅=>:用户名}} })' – Alireza 2014-10-26 19:57:55
@ali谢谢:)。这里是我使用的语法::include => {:reviews => {:include => {:user => {:only =>:username}}}}' – user2954587 2014-10-26 20:35:00