Skip to content

Instantly share code, notes, and snippets.

@ayamomiji
Created May 15, 2012 07:33
Show Gist options
  • Save ayamomiji/2699765 to your computer and use it in GitHub Desktop.
Save ayamomiji/2699765 to your computer and use it in GitHub Desktop.
resource loading patterns
class PostsController
before_filter :load_user
before_filter :load_post
def show
respond_with(@post)
end
def load_user
@user = User.find(params[:user_id])
end
def load_post
@post = @user.posts.find(params[:id])
end
end
class PostsController
def show
respond_with(post)
end
def user
@user ||= User.find(params[:user_id])
end
def post
@post ||= user.posts.find(params[:id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment