Skip to content

Instantly share code, notes, and snippets.

@webgago
Created June 24, 2011 12:15
Show Gist options
  • Save webgago/1044660 to your computer and use it in GitHub Desktop.
Save webgago/1044660 to your computer and use it in GitHub Desktop.
class Post < ActiveRecord::Base
end
class PostsController < ApplicationController
SECTIONS = %w{seo ruby php java}
def index
if params[:latest] == true
@posts = Post.where("updated_at between now() - interval '7 day' and now()").order("updated_at desc")
elsif !params[:section].nil? && SECTIONS.include?(params[:section])
@posts = Post.where("section = #{params[:section]}").order("updated_at desc")
else
@posts = Post.order("updated_at desc")
end
@comment_sizes = { }
@posts.each do |post|
@comment_sizes[post.id] = post.comments.size
end
end
def comments
if params[:latest] == true
@posts = Post.where("updated_at between now() - interval '7 day' and now()").order("updated_at desc")
elsif !params[:section].nil? && SECTIONS.include?(params[:section])
@posts = Post.where("section = #{params[:section]}").order("updated_at desc")
else
@posts = Post.order("updated_at desc")
end
@comments = []
@posts.each do |post|
@comments += post.comments
end
end
end
Simple::Application.routes.draw do
resources :posts do
collection do
get 'comments'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment