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