# Active Record From A String

**URL:** <https://www.ruby-forum.com/t/active-record-from-a-string/77353>\
**Category:** Rails\
**Created:** [November 27, 2006, 9:37am UTC](https://www.ruby-forum.com/t/active-record-from-a-string/77353 "2006-11-27T09:37:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [November 27, 2006, 9:37am UTC](https://www.ruby-forum.com/t/active-record-from-a-string/77353/1 "2006-11-27T09:37:31Z")

</div>

Hi,

I would like to create an active record object from a string that I will  
be passed. I could use eval but the documentation says that this can be  
slow. Is there any other simple way to create an record object at run  
time.

Thank,

HJS

---

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [November 27, 2006, 11:42am UTC](https://www.ruby-forum.com/t/active-record-from-a-string/77353/2 "2006-11-27T11:42:21Z")

</div>

What’s in the string ?

Fred

---

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [November 27, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/active-record-from-a-string/77353/3 "2006-11-27T21:09:18Z")

</div>

Frederick C. wrote:

> What’s in the string ?

Sorry, I mean create an active record object from a string containing  
the name of the object (which I won’t know till runtime).

HJS

---

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [November 27, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/active-record-from-a-string/77353/4 "2006-11-27T22:26:46Z")

</div>

Watch out! This can be a major security hole, if you’re not 100% sure  
of whats in my\_str.

---

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [November 27, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/active-record-from-a-string/77353/5 "2006-11-27T21:28:41Z")

</div>

H. joseph Solbrig wrote:

> Hi,
> 
> I would like to create an active record object from a string that I will  
> be passed. I could use eval but the documentation says that this can be  
> slow. Is there any other simple way to create an record object at run  
> time.
> 
> Thank,
> 
> HJS

Hi,

Generally the way to do it would be something like this:

myStr = “Post”  
posts = myStr.constantize.find(:all)

Essentially what that is doing is taking the string “Post”, converting  
it to the class with the name Post. So once you call constantize on that  
string, you can call any of Post’s methods. In the end of this code, the  
variable posts would contain a collection of all Post ActiveRecord  
objects.

---

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [December 20, 2006, 8:45am UTC](https://www.ruby-forum.com/t/active-record-from-a-string/77353/6 "2006-12-20T08:45:10Z")

</div>

Marc Baumbach wrote:

> H. joseph Solbrig wrote:
> 
> > Hi,
> > 
> > I would like to create an active record object from a string  
> > …
> 
> myStr = “Post”  
> posts = myStr.constantize.find(:all)

…

How would you find out if the class “Post” existed?

(This is probably a newby but haven’t found it in my searches…)

HJS

---

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [December 22, 2006, 1:50am UTC](https://www.ruby-forum.com/t/active-record-from-a-string/77353/7 "2006-12-22T01:50:16Z")

</div>

Just out of curiosity, do you know why constantize uses module\_eval  
and not const\_get?

If I was doing it without constantize, I would do it as:

begin  
obj = Kernel.const\_get(my\_str).new  
rescue  
puts “no class named #{my\_str}”  
end

Michael

---

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [December 22, 2006, 12:45am UTC](https://www.ruby-forum.com/t/active-record-from-a-string/77353/8 "2006-12-22T00:45:29Z")

</div>

try  
defined?(myStr.constantized) == “constant”

If it is defined, it will spit back “constant”, otherwise you could see  
“method”. Normally, if you called defined? on an actual constant that  
isnt there (i.e. defined?(Poster) ) you would get back nil, but since  
you are trying to check the output of a method, instead of returning nil  
it returns “method”

hope that helps

–jake

H. joseph Solbrig wrote:

> Marc Baumbach wrote:
> 
> > H. joseph Solbrig wrote:
> > 
> > > Hi,
> > > 
> > > I would like to create an active record object from a string  
> > > …
> > 
> > myStr = “Post”  
> > posts = myStr.constantize.find(:all)
> 
> …
> 
> How would you find out if the class “Post” existed?
> 
> (This is probably a newby but haven’t found it in my searches…)
> 
> HJS
