As an exercise, I am working on app for finding people to play RockBand with.
Users sign up as “Drummers”, “Guitarists” or “Singers”. Since the data is the same for all types, I decided to go with STI. These are my models:
class User < ActiveRecord::Base attr_accessible( :email, :name, :password, :password_confirmation, :type ) has_secure_password end class Drummer < User end class GuitarPlayer < User end class Singer < User end
My goal with this app is trying to apply design patterns, so I tried to create a UserFactory object.
It is called from the Users controller:
class UsersController < ApplicationController def new @user = User.new end def create user = UserFactory.create_with_type(params[:user]) session[:current_user] = user redirect_to dashboard_path end end
and it looks like this:
class UserFactory def self.create_with_type(params) type = params.fetch(:type) type.constantize.create(params) end end
My question is should I use a class method like I did or should I be instantiating a new UserFactory from the controller?