Changing model type


#1

I have a form that allows a User to add/edit the additional Users or
LoginUsers to/on their account, but the wrong validations are run when
updating a User to be a Login user or vice-versa.

For more clarity (with only one case shown):

class User < ActiveRecord::Base
end

class LoginUser < User
validates_presence_of :login
validates_presence_of :password
end

class UsersController < ApplicationController

def update
@user = @account.users.find(params[:id])

# revoking login privledges
if @user.is_a?(LoginUser) && !params[:allow_login]
  #switch the type here, tried a few different things but none work
  @user[:type] = "User"
end

if @user.update_attributes(params[:user])
  flash[:notice] = 'User successfully updated.'
  redirect_to(edit_account_path)
else
  # edit action rendered with errors on login and password
  render :action => "edit"
end

end

end

Can someone help me solve this?

Thanks!


#2

Jared Moody wrote:

I have a form that allows a User to add/edit the additional Users or
LoginUsers to/on their account, but the wrong validations are run when
updating a User to be a Login user or vice-versa.

# revoking login privledges
if @user.is_a?(LoginUser) && !params[:allow_login]
#switch the type here, tried a few different things but none work
@user[:type] = “User”
@user.save_without_validation!
@user = User.find(@user.id)
end

Try adding the above two lines.


Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com