【发布时间】:2012-10-16 17:05:20
【问题描述】:
在我看来,我有以下 to_json:
<%=raw @forums.to_json(:methods => [:topic_count, :last_post]) %>;
在模型中我有以下方法:
def last_post
post = ForumPost.joins(:forum_topic).where(:forum_topics => {:forum_id => self.id}).order("forum_posts.created_at DESC").first
return post
end
但是 ForumPost 包含与 ForumTopic (belongs_to :forum_topic) 的关系,我想在我的 json 中包含这个 ForumTopic,所以我最终得到 {..., "last_post":{..., "forum_topic":{ ...}...},...}。我怎样才能做到这一点?
【问题讨论】:
标签: ruby ruby-on-rails-3 activerecord ruby-on-rails-3.2