Ruby 型号 |为什么模型为空而序列化程序包含属性?
Ruby model | Why is model empty and serializer contains properties?
我是 ruby 的新手,无法理解这种情况。
我正在使用 active_model_serializers 生成模型和序列化程序。
现在运行后
$ rails g resource post title:string body:string
生成了两个文件。
- app/model/post.rb
- app/serializers/post_serializer.rb
到目前为止一切顺利。
但为什么模型对象 (post.rb) 是空的并且没有属性?
class Post < ActiveRecord::Base
end
为什么序列化器对象包含我为模型对象定义的属性?我的意思是序列化程序 -> 进行序列化的组件
class PostSerializer < ActiveRecord::Base
attributes :id, :title, :body
end
中的文档
The attribute names are a whitelist of attributes to be serialized.
Active Model 序列化程序是根据 API 需要有选择地将您的模型转换为 JSON 的方法,而不是发出 Active Model 模型的所有属性。
因此,属性在活动模型序列化程序中明确列出类
我是 ruby 的新手,无法理解这种情况。
我正在使用 active_model_serializers 生成模型和序列化程序。 现在运行后
$ rails g resource post title:string body:string
生成了两个文件。
- app/model/post.rb
- app/serializers/post_serializer.rb
到目前为止一切顺利。
但为什么模型对象 (post.rb) 是空的并且没有属性?
class Post < ActiveRecord::Base
end
为什么序列化器对象包含我为模型对象定义的属性?我的意思是序列化程序 -> 进行序列化的组件
class PostSerializer < ActiveRecord::Base
attributes :id, :title, :body
end
The attribute names are a whitelist of attributes to be serialized.
Active Model 序列化程序是根据 API 需要有选择地将您的模型转换为 JSON 的方法,而不是发出 Active Model 模型的所有属性。
因此,属性在活动模型序列化程序中明确列出类