Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Created August 23, 2011 18:14
Show Gist options
  • Save stevenharman/1166043 to your computer and use it in GitHub Desktop.
Save stevenharman/1166043 to your computer and use it in GitHub Desktop.
Trying not to let ActiveRecord leak out...
# This is pretty typical, and often 'advised' in Rails apps today
class ThingController < ApplicationController
def create
@thing = current_user.things.build(params[:thing]) # YUCK!
if @thing.save
redirect_to(user_thing_path(@thing.seller, @thing))
else
render :new
end
end
end
# I think this might be a better abstraction
class ThingController < ApplicationController
def create
seller = Seller.new(current_user)
@thing = seller.list_thing(params[:thing])
if @thing.valid?
redirect_to(user_thing_path(@thing.seller, @thing))
else
render :new
end
end
end
class Seller
initialize(user)
@user = user
end
def list_thing(thing_attrs)
@user.things.create(thing_attrs)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment