Defining an ActiveRecord class within a method of another class

G

Glenn

[Note: parts of this message were removed to make it a legal post.]

Hi,

I want to create a class and define a subclass of ActiveRecord within that class, like this:

class Results
def initialize(db)
class db < ActiveRecord::Base
end
end
end

r1 = Results.new('DM')
r2 = Results.new('AE')

Then I want to put that class definition into an object and use it in the instance methods of the Results class. (The instance methods of the Results class will do stuff with database tables, but I want to pick which table when I create the instances of the Results class.)

I am sure that the syntax above is wrong -- probably in many ways -- but am unsure how to fix it. Ruby does not seem to want to allow class definitions within methods, but if I can't do that, I do not know of another way to do this.

Thanks for your help.
 
T

Tsunami Scripter

I'm guessing this would do the trick :

irb(main):042:0> class Res
irb(main):043:1> def self.get(cls)
irb(main):044:2> eval("class #{cls};end")
irb(main):045:2> return eval("#{cls}.new")
irb(main):046:2> end
irb(main):047:1> end
irb(main):042:0> class Res
irb(main):043:1> def self.get(cls)
irb(main):044:2> eval("class #{cls};end")
irb(main):045:2> return eval("#{cls}.new")
irb(main):046:2> end
irb(main):047:1> end

You can modify what's being evaled .
 
T

Tsunami Scripter

Sorry , I pasted the same thing twice

irb(main):042:0> class Res
irb(main):043:1> def self.get(cls)
irb(main):044:2> eval("class #{cls};end")
irb(main):045:2> return eval("#{cls}.new")
irb(main):046:2> end
irb(main):047:1> end
=> nil
irb(main):048:0> x = Res.get("G")
=> #<Res::G:0xb7c05d5c>
irb(main):049:0> x.class
=> Res::G
 
J

Jesús Gabriel y Galán

Hi,

I want to create a class and define a subclass of ActiveRecord within tha= t class, like this:

class Results
def initialize(db)
class db < ActiveRecord::Base
end
end
end

r1 =3D Results.new('DM')
r2 =3D Results.new('AE')

Then I want to put that class definition into an object and use it in the=
instance methods of the Results class. (The instance methods of the Result=
s class will do stuff with database tables, but I want to pick which table =
when I create the instances of the Results class.)
I am sure that the syntax above is wrong -- probably in many ways -- but =
am unsure how to fix it. Ruby does not seem to want to allow class definit=
ions within methods, but if I can't do that, I do not know of another way t=
o do this.

Maybe this helps: I created a table named A, with id and description.
Then (you have require active_record and establish the connection):

irb(main):081:0> class Result
irb(main):082:1> def initialize table
irb(main):083:2> @db =3D Class.new(ActiveRecord::Base) do
irb(main):084:3* set_table_name table
irb(main):085:3> end
irb(main):086:2> end
irb(main):087:1> attr_reader :db
irb(main):088:1> end
=3D> nil
irb(main):089:0> a =3D Result.new("A")
=3D> #<Result:0xb7478bc0 @db=3D#<Class:0xb7478b98>(id: integer,
description: string)>
irb(main):090:0> a.db.find:)all)
=3D> [#<#<Class:0xb7478b98> id: 1, description: "test">,
#<#<Class:0xb7478b98> id: 2, description: "test 2">]

I did the set_table_name because I think you want to choose the table
name to be the parameter passed to the Result initialize method, and
skip ActiveRecord's pluralization stuff. I store the generated class
in an instance variable. Through this variable you get access to the
ActiveRecord class.

Jesus.
 
B

Brian Candler

Glenn said:
Hi,

I want to create a class and define a subclass of ActiveRecord within
that class, like this:

class Results
def initialize(db)
class db < ActiveRecord::Base
end
end
end

r1 = Results.new('DM')
r2 = Results.new('AE')

Class.new(superclass) should do what you need without need to eval
anything. This gives you an anonymous class, which you can then assign
to a constant to give it a proper class name. Something like (untested):

class Results
def initialize(name)
@klass = Results.const_set(name, Class.new(ActiveRecord::Base))
end
def query
@klass.find(...)
end
end

r1 = Results.new('DM')
Results::DM.find(...)
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top