Define a new method called week_old_posts in the User model:
def week_old_posts posts.where("created_at > ?", 1.week.ago) end
Then in your to_json call use methods , not include :
user.to_json(:methods => :week_old_posts)
:include more useful if you want to include the whole association, use methods to include parts of the associations.
source share