0
有没有什么办法可以在has_many :roles
上返回一个数组而不是对象?返回数组而不是对象在串行器中
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :nickname, :image, :roles
has_many :roles
end
谢谢。
有没有什么办法可以在has_many :roles
上返回一个数组而不是对象?返回数组而不是对象在串行器中
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :nickname, :image, :roles
has_many :roles
end
谢谢。
你可以建立在串行自己的自定义方法和使用那些在属性
class UserSerializer < ActiveModel::Serializer
attributes :role_names
def role_names
object.roles.map(&:name)
end
end
您通常使用includes。尽管我并不熟悉使用序列化器。