Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Created August 19, 2014 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgaskins/d4b705bb8aefe1878124 to your computer and use it in GitHub Desktop.
Save jgaskins/d4b705bb8aefe1878124 to your computer and use it in GitHub Desktop.
Controllers with AR vs Perpetuity
class ArticlesController < ApplicationController
# with ActiveRecord
def create
@article = Article.new(params[:article])
if @article.save
redirect_to @article, notice: 'Article created!'
else
render :new
end
end
# with Perpetuity
def create
@article = Article.new(params[:article])
if @article.valid?
article_mapper.insert @article
redirect_to @article, notice: 'Article created!'
else
render :new
end
end
# I memoize mappers to get the benefit of their identity map
def article_mapper
@article_mapper ||= Perpetuity[Article]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment