Skip to content

Instantly share code, notes, and snippets.

@elben
Created January 30, 2010 20:31
Show Gist options
  • Save elben/290713 to your computer and use it in GitHub Desktop.
Save elben/290713 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
attr_accessor :role
has_many :user_roles, :dependent => :destroy
has_many :roles, :through => :user_roles
after_save :set_role
def set_role
if @role # NOTE (A): @role is nil. I want it to be params[:user][:role].
# NOTE (B): do stuff here, mostly:
self.roles << @role
end
end
end
...
# in users_controller.rb:
def update
@user = Uesr.find_by_login(params[:id])
...
# at this point, params[:user][:role] has a value, the one given in the form!
@user.update_attributes(params[:user])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment