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
on 2009-05-11 19:00
on 2009-05-11 19:51
> @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,.........
on 2009-05-11 20:39
Great - thanks!
on 2009-05-11 20:48
which can get you to some cool code like: @object = params[:controller].singularize.camelcase.constantize.find(params[:id])
on 2009-05-15 19:27
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"? :-) How would the following need to be corrected? (@u+ser).objectize = User.find(:first) Thanks for your help! Tom
on 2009-05-15 19:35
instance_variable_set(@u + ser, User.find(:first))
on 2009-05-15 21:25