# Can't dynamically create an Acitve::Record object

**URL:** <https://www.ruby-forum.com/t/cant-dynamically-create-an-acitve-record-object/69256>\
**Category:** Rails\
**Created:** [September 19, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/cant-dynamically-create-an-acitve-record-object/69256 "2006-09-19T18:57:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [September 19, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/cant-dynamically-create-an-acitve-record-object/69256/1 "2006-09-19T18:57:26Z")

</div>

Can anyone explain why the following shows that when you dynamically  
create an Active::Record object, the accessors/mutators for the columns  
are not created? Very puzzled! Thanks :o)

```
        klass = Class.new(ActiveRecord::Base) do
          set_table_name table_name
        end
        Kernel.const_set(model_class_name, klass)

        puts "Created the model class and set as a constant."

        column_names = model_class.columns.map{ |x| x.name }
        methods = model_class_name.constantize.new.methods
        raise "Columns not in instance as methods\n\n"+
           "Columns:\n\n#{column_names.sort.join("\n")}\n\n"+
           "Instance Methods:\n\n#{methods.sort.join("\n")}"
           if methods & column_names != column_names
```

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [September 19, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/cant-dynamically-create-an-acitve-record-object/69256/2 "2006-09-19T21:20:56Z")

</div>

On 9/19/06, Matthew P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone explain why the following shows that when you dynamically  
> create an Active::Record object, the accessors/mutators for the columns  
> are not created? Very puzzled! Thanks :o)

Try it with a ‘normal’ AR object - doesn’t work either. Attribute  
getters/setters are handled by method\_missing, which defines instance  
methods at runtime for speed.

Otherwise, Object.const\_set :Person, Class.new(ActiveRecord::Base) is  
plenty.

jeremy

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [September 19, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/cant-dynamically-create-an-acitve-record-object/69256/3 "2006-09-19T19:40:43Z")

</div>

On 9/19/06, Matthew P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone explain why the following shows that when you dynamically  
> create an Active::Record object, the accessors/mutators for the columns  
> are not created? Very puzzled! Thanks :o)

I’m not sure what’s wrong with the code you listed, but here’s a module  
I  
use to create dynamic active record objects.

module Stone  
module Dynamic  
class \<\< self  
def klass(table\_name)  
tname = class\_name\_from\_table(table\_name)  
#see if it has already been defined  
const\_missing(tname)  
rescue NameError  
define\_klass(table\_name)  
end

```
  def objeck(table_name)
    klass(table_name).new
  end

  private
    def class_name_from_table(table_name)
      Inflector.camelize(table_name)
    end

    def define_klass(table_name)
      tname = class_name_from_table(table_name)
      class_def = <<-end_eval
        class #{tname} < ActiveRecord::Base
          set_table_name('#{table_name}')
        end
      end_eval
      eval(class_def, TOPLEVEL_BINDING)
      const_get(tname)
    end
end

```

end  
end

Hope this is of some use,  
andy

–  
Andrew S.

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [September 20, 2006, 11:26am UTC](https://www.ruby-forum.com/t/cant-dynamically-create-an-acitve-record-object/69256/4 "2006-09-20T11:26:35Z")

</div>

Thanks guys!
