# Class constants

**URL:** <https://www.ruby-forum.com/t/class-constants/126030>\
**Category:** Rails\
**Created:** [January 14, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/class-constants/126030 "2008-01-14T21:45:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [January 14, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/class-constants/126030/1 "2008-01-14T21:45:45Z")

</div>

If I have:

@name = ‘users’

And I want to do

@name.singularize.camelize.constantize.find(1)

It seems I cannot do so unless the constant User is already present in  
ObjectSpace. Is there a dynamic way around this?

I’m embarrassed to admit this is my best idea so far:

name = @name.singularize  
open( “#{ RAILS\_ROOT }/tmp/cache/#{ name }.rb”, ‘w’ ) do |f|  
f.puts “class #{ name.camelize } \< ActiveRecord::Base\n  
set\_table\_name "#{ @name }"\nend\n”  
end  
require “#{ RAILS\_ROOT }/tmp/cache/#{ name }.rb”  
name = name.camelize.constantize

It works fine but then I have to do:

Dir.glob(“#{RAILS\_ROOT}/app/models/\*.rb”).each { |f| require f }

at the end to restore sanity to my Rails app.

I realize I can add some more checks to see if the faked model file is  
already there and all, but please someone tell me there’s a better  
way.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [January 14, 2008, 9:56pm UTC](https://www.ruby-forum.com/t/class-constants/126030/2 "2008-01-14T21:56:56Z")

</div>

On Jan 14, 12:44 pm, “Greg D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If I have:
> 
> @name = ‘users’
> 
> And I want to do
> 
> @name.singularize.camelize.constantize.find(1)
> 
> It seems I cannot do so unless the constant User is already present in  
> ObjectSpace. Is there a dynamic way around this?

require @name ?

///ark

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [January 14, 2008, 9:59pm UTC](https://www.ruby-forum.com/t/class-constants/126030/3 "2008-01-14T21:59:17Z")

</div>

On Jan 14, 12:44 pm, “Greg D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If I have:
> 
> @name = ‘users’
> 
> And I want to do
> 
> @name.singularize.camelize.constantize.find(1)

How about:

require @name.singularize

///ark

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [January 14, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/class-constants/126030/4 "2008-01-14T22:07:30Z")

</div>

On Jan 14, 2008 2:56 PM, Mark W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require @name ?

That works fine if you have the model file already.

Please re-read my post… or not.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [January 14, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/class-constants/126030/5 "2008-01-14T22:08:05Z")

</div>

On 14 Jan 2008, at 20:44, Greg D. wrote:

> ObjectSpace. Is there a dynamic way around this?

Is there no constant just because the user.rb file has not been loaded  
or is there no user.rb file  
If the former it should just work, if the latter:

klass = Class.new(ActiveRecord::Base)  
Object.const\_set name.camelize, klass  
klass.reset\_table\_name

klass.find :first

Fred

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [January 14, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/class-constants/126030/6 "2008-01-14T22:36:28Z")

</div>

On Mon, Jan 14, 2008 at 02:44:47PM -0600, Greg D. wrote:

> It works fine but then I have to do:
> 
> Dir.glob("#{RAILS\_ROOT}/app/models/\*.rb").each { |f| require f }
> 
> at the end to restore sanity to my Rails app.
> 
> I realize I can add some more checks to see if the faked model file is  
> already there and all, but please someone tell me there’s a better  
> way.

If I understand correctly, you are looking for a way to wrap an  
ActiveRecord model around a DB table given the name of the DB table  
without  
(necessarily) creating a .rb file for it a priori. If that’s accurate, I  
think you want this:

name = Object.const\_set(@name.singularize.camelize,  
Class.new(ActiveRecord::Base))

> Greg D.  
> –Greg

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [January 14, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/class-constants/126030/7 "2008-01-14T23:11:41Z")

</div>

On Jan 14, 1:06 pm, “Greg D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jan 14, 2008 2:56 PM, Mark W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > require @name ?
> 
> That works fine if you have the model file already.
> 
> Please re-read my post… or not.

I did reread your post, which is why I deleted my message about 30  
seconds after I posted it. 🙂

///ark

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [January 15, 2008, 2:46am UTC](https://www.ruby-forum.com/t/class-constants/126030/8 "2008-01-15T02:46:50Z")

</div>

On Jan 14, 2008 3:07 PM, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> klass = Class.new(ActiveRecord::Base)  
> Object.const\_set name.camelize, klass  
> klass.reset\_table\_name
> 
> klass.find :first

That’s it. Thanks.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
