I am building a system with a User model (authenticated using AuthLogic) and three user types in three models: one of these models is Employer. Each of these three models has_many :users, :as = :authenticable.
I start by having a new visitor to the site create their own 'User' record with username, password, which user type they are, etc.
Upon creation, the user is sent to the 'new' action for one of the three models. So, if they tell us they are an employer, we redirect_to :controller = "employers, :action = "new".
Question: When the employer has submitted, I want to set the current_user.user_type_id equal to the employer ID. This should be simple... but it's not working.
# Employers Controller / new
def new
@employer = Employer.new
1.times {@employer.addresses.build}
render :layout => 'forms'
end
# Employers Controller / create
def create
@employer = Employer.new(params[:employer])
if @employer.save
if current_user.blank?
redirect_to :controller => "users", :action => "new"
else
current_user.user_type_id = @employer.id
current_user.user_type = "Employer"
redirect_to :action => "home", :id => current_user.user_type_id
end
else
render :action => "new"
end
end
------UPDATE------
Hi guys. In response:
I am using this table structure because each of my three user type models have lots of different fields and each has different relationships to the other models, which is why I've avoided STI.
By 1.times (@employer.addresses.build) I'm connecting the employer model to the address polymorphic table in one form, so I'm asking the controller to build a new address to go along with the new employer.
Averell: you mentioned encapsulating... something in the model using a 'setter' method. I have no idea what you mean by this - could you please explain how this works (or direct me to an example elsewhere)? With tsdbrown's answer I have managed to create the behavior I want... if there's a more elegant way to accomplish the same thing I'd love to learn how.
Thanks very much.
Thanks to tsdbrown for answering the current_user.save problem!