Forum: Ruby on Rails Query question

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.
460ead051b49c0b2a8e45e46252d4bda?d=identicon&s=25 Tom Ha (tom)
on 2009-05-11 19:00
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
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-05-11 19:51
(Received via mailing list)
>  @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,.........
460ead051b49c0b2a8e45e46252d4bda?d=identicon&s=25 Tom Ha (tom)
on 2009-05-11 20:39
Great - thanks!
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2009-05-11 20:48
which can get you to some cool code like:

@object =
params[:controller].singularize.camelcase.constantize.find(params[:id])
460ead051b49c0b2a8e45e46252d4bda?d=identicon&s=25 Tom Ha (tom)
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
885ac43bca92fc2b1034356f3283e788?d=identicon&s=25 pharrington (Guest)
on 2009-05-15 19:35
(Received via mailing list)
instance_variable_set(@u + ser, User.find(:first))
460ead051b49c0b2a8e45e46252d4bda?d=identicon&s=25 Tom Ha (tom)
on 2009-05-15 21:25
Thanks!
This topic is locked and can not be replied to.