Query question


#1

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


#2

@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,…


#3

Great - thanks!


#4

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


#5

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


#6

which can get you to some cool code like:

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


#7

Thanks!