Skip to content

Instantly share code, notes, and snippets.

@jcasimir
jcasimir / sessions_and_conversations.markdown
Created September 11, 2011 23:07
Sessions and Conversations in Rails 3

Sessions and Conversations

HTTP is a stateless protocol. Sessions allow us to chain multiple requests together into a conversation between client and server.

Sessions should be an option of last resort. If there's no where else that the data can possibly go to achieve the desired functionality, only then should it be stored in the session. Sessions can be vulnerable to security threats from third parties, malicious users, and can cause scaling problems.

That doesn't mean we can't use sessions, but we should only use them where necessary.

Adding, Accessing, and Removing Data

@jcasimir
jcasimir / friendly_urls.markdown
Created September 11, 2011 15:48
Friendly URLs in Rails

Friendly URLs

By default, Rails applications build URLs based on the primary key -- the id column from the database. Imagine we have a Person model and associated controller. We have a person record for Bob Martin that has id number 6. The URL for his show page would be:

/people/6

But, for aesthetic or SEO purposes, we want Bob's name in the URL. The last segment, the 6 here, is called the "slug". Let's look at a few ways to implement better slugs.

@agibralter
agibralter / config-initializers-model_tracker.rb
Created December 28, 2009 07:14
Turning off transactional fixtures for specific RSpec example groups and a ThinkingSphinx search use-case.
if Rails.env.features? || Rails.env.test?
ActiveRecord::Base.class_eval do
class << self
def delete_all_of_all
@model_subclasses_for_delete_all_of_all.each do |klass|
klass.delete_all
end
end
def inherited_with_model_tracker(subclass)
@model_subclasses_for_delete_all_of_all ||= []