Ruby on rails 如何更改我的as_json方法?

Ruby on rails 如何更改我的as_json方法?,ruby-on-rails,json,Ruby On Rails,Json,现在我已经在我的模型中使用了这样的as_json方法 def as_json(options = {}) { id: id, diary_id: diary_id, title: title, post_date_gmt: date, post_content: strip_tags(content), smiley_id: smiley_id, author_id: user_id, author_name: user.dis

现在我已经在我的模型中使用了这样的as_json方法

  def as_json(options = {})
{
    id: id,
    diary_id: diary_id,
    title: title,
    post_date_gmt: date,
    post_content: strip_tags(content),
    smiley_id: smiley_id,
    author_id: user_id,
    author_name: user.display_name,
    attachments: filter_attachments(options[:version]),
    root_comments: format_comments(nested_comments.arrange(:order => :created_at)),
    post_readings: post_readings.size,
    is_read: read_by(options[:current_user])
}
end
我需要如下改变这个结构,实际上我想按日期分组这个数组

{
  date_01: {
    [post1], [post2], [post3]
   },
  date_02: {
    [post1], [post2], [post3]
   }
}

我应该怎么做?

将数据键的值替换为数组数组。如下图所示

{
  date_01: [
    [post1], [post2], [post3]
   ],
  date_02: [
    [post1], [post2], [post3]
   ]
}

我解决了这个问题如下

post_dates = (no_of_days.days.ago.to_date..(date_as_string.to_date + no_of_days.days)).map{ |date| date.strftime("%Y-%m-%d") }

# Arrange posts details under each date
  i = 0
  post_dates.each do |post_date|
    posts_grouped_by_date[i] = {:post_date => post_date, :posts_for_date => diary.posts_for_date(Date.parse(post_date)  )}
    i = i + 1
  end

render json: posts_grouped_by_date.sort_by {|hash| hash['post_date']}.as_json(current_user: current_user)