Using a result as a constant object?

P

Phy Prabab

Hello,

I am trying to understand how I can use a return from a method as a constant name for a class. What I am trying to do is take a return I find in a db table and use that name as a class name so that I can use activerecord. I admit my understanding of ActiveRecord is extremely immature so perhaps I am using it wrong.
...
perMatrtix = subMatrix.find[:space].to.upcase

class perMatrix < ActiveRecord::Base; end

blah, blah, blah...

So the return of the find, which is a name (turn to upper) as the name of a class so that I can access that table within the database.

Any help is greatly appreciated! Cheers,
Phy

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
 
T

Tim Hunter

Phy said:
Hello,

I am trying to understand how I can use a return from a method as a constant name for a class. What I am trying to do is take a return I find in a db table and use that name as a class name so that I can use activerecord. I admit my understanding of ActiveRecord is extremely immature so perhaps I am using it wrong.
...
perMatrtix = subMatrix.find[:space].to.upcase

class perMatrix < ActiveRecord::Base; end

blah, blah, blah...

So the return of the find, which is a name (turn to upper) as the name of a class so that I can access that table within the database.

If you're getting the class name at run-time how would you write code
that uses class after you've created it? I mean, you can't type the
class name into your program now, you don't know what it is?

I suspect that what you really want to do is simply create a class and
assign the result to a variable. Check out Class.new.

my_class = Class.new(ActiveRecord::Base)
my_inst = my_class.new
 
A

Artem Voroztsov

2008/10/19 Tim Hunter said:
Phy said:
Hello,

I am trying to understand how I can use a return from a method as a
constant name for a class. What I am trying to do is take a return I find
in a db table and use that name as a class name so that I can use
activerecord. I admit my understanding of ActiveRecord is extremely
immature so perhaps I am using it wrong.
...
perMatrtix = subMatrix.find[:space].to.upcase

class perMatrix < ActiveRecord::Base; end

blah, blah, blah...

So the return of the find, which is a name (turn to upper) as the name of
a class so that I can access that table within the database.

If you're getting the class name at run-time how would you write code that
uses class after you've created it? I mean, you can't type the class name
into your program now, you don't know what it is?

I suspect that what you really want to do is simply create a class and
assign the result to a variable. Check out Class.new.

my_class = Class.new(ActiveRecord::Base)
my_inst = my_class.new

my_class = Class.new(ActiveRecord::Base) { set_table_name
subMatrix.find[:space].underscore.pluralize }
my_inst = my_class.new

Artem

PS:
Expression "subMatrix.find[:space]" seems strange for me. What is it about?
 
S

Sjoerd Andringa

If the ActiveRecord model already exists you can just call constantize
on the string containing the model name:

class SomeModel < ActiveRecord::Base; end

model_name = "SomeModel" # or in your case fetch this from a database
ar_model = model_name.constantize
ar.model.find(120)
 
S

Sjoerd Andringa

Sjoerd said:
Should be: ar_model.find(120)

Be aware that 'constantize' is a method added by ActiveSupport, no
problem if you're using Rails, but you need to require it manually
otherwise.
 
B

Brian Candler

Phy said:
I am trying to understand how I can use a return from a method as a
constant name for a class.

def get_name
"Foo"
end

n = get_name
c = Class.new(ActiveRecord::Base) # superclass
Object.const_set(n, c)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top