是否可以在rails 5.2.x中渲染像这样的json?
目前,我正在学习创建Rails api项目。
我试过不添加messages.user
,效果很好
#user model
class User < ApplicationRecord
has_many :messages
belongs_to :room
end
#room model
class Room < ApplicationRecord
has_one :user
has_many :messages, -> { order('created_at DESC') }
end
#message model
class Message < ApplicationRecord
belongs_to :user
belongs_to :room
end
#controller
class RoomsController < ApplicationController
def show
render json: {
status: 'success',
code: 200,
data: @room
}, include: ['user','messages','messages.user']
end
end
我希望json结构的输出会像这样
{
-room (room info)
--user (receiver data/room owner)
--message (message content)
---user (sender data)
}
但是,这就是这样的返回错误
NoMethodError:#
的未定义方法`messages.user'