Dynamically Create a Class Inherited from Activerecord

Dynamically create a class inherited from ActiveRecord?

Nailed it:

def create_arec(table_name, &block)
klass = Class.new(ActiveRecord::Base){self.table_name = table_name}
klass.class_eval &block
klass
end

thanks @phoet

Using dynamically created classes in a Single Table Inheritance mechanism

Your error message stands that 'FooObject' class cannot be located.

In your code, the dynamic generated class name shoudl be 'FooDynObject'.

Just check you don't have old test records in your database before loading DynObject.

@edit:
Another thing is also to know on which class you affect the dynamic class name.

class DynObject < ActiveRecord::Base
const_set 'FooDynObject', Class.new(DynObject)
end

Will result in DynObject::FooDynObject, and ActiveRecord won't be able to load it when it will see 'FooDynObject' type.

Personnally, I would do someting like

class DynObject < ActiveRecord::Base
Object.const_set 'FooDynObject', Class.new(DynObject)
end

Dynamically create Ruby class with ActiveRecord connection

You may set establish_connection for model dynamically:

database.yml

development:
adapter: mysql
username: root
password:
database: example_development

oracle_development:
adapter: oracle
username: root
password:
database: example_oracle_development

And in any place of code you may change db connection of your model:

User.establish_connection "oracle_#{RAILS_ENV}".to_sym

Also you can create model classes dynamically:

class ClassFactory
def self.create_class(new_class, table, connection_name)

Object.const_set(new_class, Class.new(ActiveRecord::Base) {})
new_class.constantize.table_name = table
new_class.constantize.establish_connection(connection_name)

end
end

ClassFactory.create_class('NewUser', 'users', :development)

After that NewUser class will be available to use.

This version works in both Rails 5.0 and 3.2.

How to dynamically alter inheritance in Ruby

Joshua has already given you a great list of alternatives, but to answer your question: You can't change the superclass of a class after the class has been created in ruby. That's simply not possible.

Ruby: How to dynamically create a subclass of an existing class?

Class.new accepts a parameter, which will be the superclass.

Documentation: Class.new.

Ruby Generating Subclass at runtime

Do you look for `Class.new(Item)``

Example:

require 'active_record'

class Item < ActiveRecord::Base
#template methods
end

x = Class.new(Item)
puts x.ancestors

One of the ancestors is Item.

By the way: Your Class StoreItem < Item is wrong. You must use class instead of Class in this case.

How can I open a class and convert it into ActiveRecord object? (for example, make it inherit ActiveRecord::Base)

You cannot change the superclass of a class. (Well, technically, Module#include calls Module#append_features, which in most implementations creates an include class and makes that class the superclass of the class that include is called on, but you cannot do that in user code.)

You will have to either change your original class definition or create a new class. There is no way around that.



Related Topics



Leave a reply



Submit