How to create an ActiveRecord dynamic creation class during runtime? (ruby)

I am trying metaprogramming and want to dynamically create a class that inherits from ActiveRecord.

For example, I can do this:

< p>

Object.const_set("Orders", Class.new {def blah() 42 end })

So now I can:

o = Orders.new
o.blah #<== 42

But when I try:

Object. const_set("Orders", Class.new 

Give me a syntax error and

 Object.const_set("Orders", Class.new {def blah() 42 end} 

I won’t complain until I try to instantiate the Orders class

Is there a tip?

Thank you.

try this:

SomeClass = Class.new(ActiveRecord::Base) do
.... #some behaviour
end

I am trying metaprogramming and want to dynamically create a class that inherits from ActiveRecord.

For example, I can do this:

Object.const_set("Orders", Class.new {def blah() 42 end })

So now I can:

o = Orders .new
o.blah #<== 42

But when I try:

Object.const_set("Orders", Class. new 

Give me a syntax error and

Object.const_set("Orders", Class.new {def blah() 42 end} 

I won’t complain until I try to instantiate the Orders class

Are there any tips?

Thank you.

Try this:

SomeClass = Class.new(ActiveRecord::Base) do
.... #some behaviour
end

Leave a Comment

Your email address will not be published.