Forum: Ruby converting class or type under single table inheritance

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.
2e47e5cb83bfb29e87c6aa1495b9837a?d=identicon&s=25 Levent Ali (levent)
on 2006-05-22 10:48
I think this query is more Ruby than Rails but I'm writing a Rails app
using single table inheritance

class User < ActiveRecord::Base
end

class AdminUser < User
end

The type column in the users table holds the class name for each
instance

Now I want to convert a User object into an AdminUser.  Is this
possible?  Does anyone know the best way to do this?

Thanks in advance
levent
5d15f83f0e681b138d95f1ec430868fa?d=identicon&s=25 Joey (Guest)
on 2006-05-22 11:21
(Received via mailing list)
user[:type] = AdminUser
2e47e5cb83bfb29e87c6aa1495b9837a?d=identicon&s=25 Levent Ali (levent)
on 2006-05-22 11:43
Joey wrote:
> user[:type] = AdminUser

that doesn't seem to work.. that throws "TypeError: can't dump anonymous
class Class"

I've already tried stuff like:
@user = User.find(1) # an object of type User not AdminUser
@user.type = AdminUser
@user.save # Throws the same error as above

@user[:type] = AdminUser
@user.save # Throws the same error
Bf6862e2a409078e13a3979c00bba1d6?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-05-22 13:51
(Received via mailing list)
On Mon, May 22, 2006 at 06:43:31PM +0900, Levent Ali wrote:
} Joey wrote:
} > user[:type] = AdminUser
}
} that doesn't seem to work.. that throws "TypeError: can't dump
anonymous
} class Class"
}
} I've already tried stuff like:
} @user = User.find(1) # an object of type User not AdminUser
} @user.type = AdminUser
} @user.save # Throws the same error as above
}
} @user[:type] = AdminUser
} @user.save # Throws the same error

You can't set it to the class, you need to set it to the name. It's a
subtlety:

user[:type] = 'AdminUser'

--Greg
2e47e5cb83bfb29e87c6aa1495b9837a?d=identicon&s=25 Levent Ali (levent)
on 2006-05-22 13:58
Gregory Seidman wrote:
> On Mon, May 22, 2006 at 06:43:31PM +0900, Levent Ali wrote:
> } Joey wrote:
> } > user[:type] = AdminUser
> }
> } that doesn't seem to work.. that throws "TypeError: can't dump
> anonymous
> } class Class"
> }
> } I've already tried stuff like:
> } @user = User.find(1) # an object of type User not AdminUser
> } @user.type = AdminUser
> } @user.save # Throws the same error as above
> }
> } @user[:type] = AdminUser
> } @user.save # Throws the same error
>
> You can't set it to the class, you need to set it to the name. It's a
> subtlety:
>
> user[:type] = 'AdminUser'
>
> --Greg

I'm an idiot :)

Thanks.. I can't believe I didn't think of that.. that's what happens
when you spend hours on a simple thing, over thinking the problem..
thanks again

levent
This topic is locked and can not be replied to.