Query question

Hi there,

Why does the following query…

ser = ‘ser’
@user = (‘U’+ser).find(:first)

…not equal this one:

@user = User.find(:first)

?

What needs to be done so it will work?

Thanks for your help!
Tom

@user = User.find(:first)

?

What needs to be done so it will work?

ser = ‘ser’
=> “ser”

user = ‘U’ + ser
=> “User”

user.class
=> String

User.class
=> Class

user.constantize.class
=> Class

user.constantize.find(:first)
User Load (1.2ms) SELECT * FROM “users” LIMIT 1
=> #<User id: 1,…

Great - thanks!

1 question back, though:

What if need to end up having…

@user = User.find(:first)

…based on a case like this:

@u = ‘@u
ser = ‘ser’

Is there something as “objectize”? :slight_smile:

How would the following need to be corrected?

(@u+ser).objectize = User.find(:first)

Thanks for your help!
Tom

instance_variable_set(@u + ser, User.find(:first))

which can get you to some cool code like:

@object =
params[:controller].singularize.camelcase.constantize.find(params[:id])

Thanks!