【发布时间】:2019-08-14 09:17:03
【问题描述】:
我正在构建一个具有 Users、Sightings 和 Comments 的 Rails 后端。模型Comment 加入Sighting 和User。我想将用户名或用户名连同作为CommentSerializer 中连接表Comments 的属性的user_id 一起传递到我的前端。
我可以使用 Active Record 或 Ruby 方法访问这些数据,但实际上如何使这个属性成为通过 Serializer 传递的属性?
这是我的CommentSerializer 文件:
class CommentSerializer < ActiveModel::Serializer
attributes :id, :body, :likes, :user_id
belongs_to :commentable, :polymorphic => true
belongs_to :sighting
has_many :comments, as: :commentable
end
【问题讨论】:
标签: ruby-on-rails activerecord attributes serialization