Forum: Ruby on Rails changing model type

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F0432c26bb25c111188ab3acfce644ac?d=identicon&s=25 Jared Moody (jaredmoody)
on 2009-01-28 01:13
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!
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2009-01-28 03:22
(Received via mailing list)
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
This topic is locked and can not be replied to.