简体   繁体   English

Rails 4:ActiveModelSerializer如何只包括那些被批准的记录?

[英]Rails 4: ActiveModelSerializer how to include only those records which are approved?

In my Rails 4.2 API, I'm using active model serializers for constructing json response. 在我的Rails 4.2 API中,我正在使用活动模型序列化程序来构造json响应。 Suppose Post is a model and it has many comments and I only want to include comments which are approved/published. 假设Post是一个模型,并且有很多comments ,我只想包含已批准/发布的评论。 I'm using a scope called approved which gives approved comments. 我正在使用一个称为approved的范围,它给出了认可的评论。

JSON response for post includes all comments, how do I include records which are approved and not everything. 发布的JSON响应包含所有注释,我如何包含已批准的记录,而不是所有记录。 How do I construct Post serializer for that. 我该如何构造Post序列化程序。

class PostSerializer < ActiveModel::Serializer
  attributes :name, :body
  has_many :comments
end
class PostSerializer < ActiveModel::Serializer
  attributes :name, :body
  has_many :comments

  def comments
    object.comments.where( status: 'approved' )
  end
end

See Active Model Serializers - overriding association methods 请参阅活动模型序列化器-覆盖关联方法

Overriding associations in your serializer will work. 序列化程序中的覆盖关联将起作用。 In serializer just override with this method 在序列化器中只需使用此方法覆盖

def comments   
  #Your comments filtering 
end

If that doesn't work then that has got to be some issue with your version of serializer. 如果那不起作用,那一定是您的序列化程序版本有问题。 Look at this issue for more details and workarounds. 请查看此问题以获取更多详细信息和解决方法。 https://github.com/rails-api/active_model_serializers/issues/267 https://github.com/rails-api/active_model_serializers/issues/267

Check this out too. 也检查一下。 How do I select which attributes I want for active model serializers relationships 如何为活动模型序列化程序关系选择所需的属性

 class PostSerializer < ActiveModel::Serializer
   attributes :name, :body
   has_many :approved_comments, -> { where status: 'approved' }, class_name: 'Comment'
 end

 PostSerializer.includes(:approved_comments)

Scoping with the approved_comments . 范围approved_comments Fetching only the comments with the status of approved . 仅获取状态为approved的评论。 Got the concept from this http://apidock.com/rails/ActiveRecord/Associations/ClassMethods http://apidock.com/rails/ActiveRecord/Associations/ClassMethods获得了概念

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅显示特定字段不为空的那些Rails记录? - How to display only those Rails records where a specific field is not empty? 仅当在Rails中获得批准时,才如何创建草稿并进行发布? - How to create drafts and publish only if approved in Rails? 仅当在内部调用了许多时,才包含ActiveModelSerializer的属性 - Include properties for ActiveModelSerializer only if called within has many 如何使用 ActiveModelSerializer rails 4 为同一模型使用多个序列化程序 - How to use multiple Serializers for the same model with ActiveModelSerializer rails 4 rails通过检查关联是否被批准来获取记录列表 - rails get a list of records by checking if association is approved Rails和ActiveModelSerializer:合并两个集合 - Rails and ActiveModelSerializer: Merging two collections 如何在响应中仅包含包含Ruby On Rails中关联记录的最后一条记录? - How do I include only last record in the response that contains associated records in Ruby On Rails? 如何有效地为Rails模型设置批准,未批准和草稿? - How to effectively set approved, non approved and drafts for a Rails model? 关于仅获取字段值仅包含一个特定字符在 rails 中出现的那些记录的问题 - Issue regarding fetching only those records where field value contains only one occurrence of a particular character in rails 如何使用ps_search gem for Rails 5仅搜索批准的帖子 - How to search only approved posts using ps_search gem for Rails 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM