Ruby on rails 活动模型序列化程序调用带有参数的方法的嵌套关联

Ruby on rails 活动模型序列化程序调用带有参数的方法的嵌套关联,ruby-on-rails,active-model-serializers,Ruby On Rails,Active Model Serializers,例如,如果我有这些关联的模型 class User has_many :posts has_many :comments def posts_has_comments_in_certain_day(day) posts.joins(:comments).where(comments: { created_at: day }) end end class Post has_many :comments belongs_to :user def comm

例如,如果我有这些关联的模型

class User 
  has_many :posts
  has_many :comments

  def posts_has_comments_in_certain_day(day)
    posts.joins(:comments).where(comments: { created_at: day })
  end 
end

class Post
  has_many :comments
  belongs_to :user

  def comments_in_certain_day(day)
    Comment.where(created_at: day, post_id: id)
  end
end

class Comment
  belongs_to :user
  belongs_to :post
end
现在,我希望活动模型序列化程序能够让我获得所有用户的帖子,这些帖子在某一天也有评论,包括这些评论

class PostSerializer < ActiveModel::Serializer
  attributes :id, :body, :day_comments

  def day_comments
    object.comments_in_certain_day(day).map do |comment|
        CommentSerializer.new(comment).attributes
    end
  end
end
我已经试过了,但我能得到的只是用户在某一天发表的评论。。但是我不能把评论也包括进去

class PostSerializer < ActiveModel::Serializer
  attributes :id, :body, :day_comments

  def day_comments
    object.comments_in_certain_day(day).map do |comment|
        CommentSerializer.new(comment).attributes
    end
  end
end
我就是这么做的

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :day_posts

  def day_posts
    object.posts_has_comments_in_certain_day(day)
  end
end
class UserSerializer
这个很好用 但是当我尝试加入评论时

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :day_posts

  def day_posts
    object.posts_has_comments_in_certain_day(day).map do |post|
    PostSerializer.new(
      post,
      day: instance_options[:day]
    )
  end
end

class PostSerializer < ActiveModel::Serializer
  attributes :id, :body, :day_comments

  def day_comments
    object.comments_in_certain_day(day)
  end
end
class UserSerializer

这不管用。。有人能帮我吗

在序列化程序实例上调用
.attributes

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :day_posts

  def day_posts
    object.posts_has_comments_in_certain_day(day).map do |post|
    PostSerializer.new(
      post,
      day: instance_options[:day]
    ).attributes
  end
end
class UserSerializer
如果需要自定义注释属性,也可以对注释执行相同的操作

class PostSerializer < ActiveModel::Serializer
  attributes :id, :body, :day_comments

  def day_comments
    object.comments_in_certain_day(day).map do |comment|
        CommentSerializer.new(comment).attributes
    end
  end
end
class PostSerializer
对不起,我做错了什么。。它现在工作得很好,谢谢,有没有一种方法可以序列化嵌套对象而不显式地为子对象调用序列化器?要手动做这样的事情似乎有点不方便