# Inheriting from Activerecord classes

**URL:** <https://www.ruby-forum.com/t/inheriting-from-activerecord-classes/113651>\
**Category:** Rails\
**Created:** [September 3, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/inheriting-from-activerecord-classes/113651 "2007-09-03T15:01:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sudarshan\_Purohit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@Sudarshan\_Purohit](https://www.ruby-forum.com/u/Sudarshan_Purohit)\
**Post date:** [September 3, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/inheriting-from-activerecord-classes/113651/1 "2007-09-03T15:01:30Z")

</div>

Hi,  
I have this ‘legacy’ RoR application, in which, at one place,  
we’re doing something like this :

* * *

class Car \< ActiveRecord::Base

#lots of belongs\_to and has\_many relations  
belongs\_to :brand

def make\_of\_car  
#return some string…  
end

def Car.model\_factory(modelname)  
#modelname is a string  
klass = Object.const\_set(modelname, Class.new(Car))

klass.class\_eval do  
#more definitions specific to the dynamically generated model  
validates\_presence\_of something  
end

…

## end

Now this was running fine with the old RoR version we had, 1.1.4. I  
started using 1.2.3 yesterday, and suddenly, the dynamically generated  
models don’t have the methods defined within Car, in them. I mean to  
say that these became invalid:

ModelT.brand  
ModelT.make\_of\_car

The only properties that ModelT has are the ones that it get from  
ActiveRecord.

There haven’t been any code changes since we migrated. So either  
there’s something wrong with ActiveRecord/Rails, or there’s something  
wrong with our code that used to slip through ;). Could someone please  
help out, or point me to some resource that could?

Thanks in advance,  
Sudarshan

---

<div class="post-metadata">

**Author:** ![Sudarshan\_Purohit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@Sudarshan\_Purohit](https://www.ruby-forum.com/u/Sudarshan_Purohit)\
**Post date:** [September 3, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/inheriting-from-activerecord-classes/113651/2 "2007-09-03T15:19:32Z")

</div>

Sudarshan Purohit wrote:

> ModelT.brand  
> ModelT.make\_of\_car
> 
> The only properties that ModelT has are the ones that it get from  
> ActiveRecord.
> 
> There haven’t been any code changes since we migrated. So either  
> there’s something wrong with ActiveRecord/Rails, or there’s something  
> wrong with our code that used to slip through ;). Could someone please  
> help out, or point me to some resource that could?

How are your tests doing?

Can you add a method\_missing to ModelT, and get these methods to work  
again?

Can you edit boot.rb, go back to 1.1.4, pass all tests, tick to 1.1.5,  
pass  
all tests, and keep going until the tests break?

Can you diff -r the two versions of ActiveRecord to see what got  
removed?

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![Sudarshan\_Purohit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@Sudarshan\_Purohit](https://www.ruby-forum.com/u/Sudarshan_Purohit)\
**Post date:** [September 4, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/inheriting-from-activerecord-classes/113651/3 "2007-09-04T15:19:15Z")

</div>

Hi,

> How are your tests doing?

I’m ashamed to say that they’re passing - because they don’t cover  
this particular Model class(es). It’s the only class that uses  
class.new, though, so other stuff passes through the tests fine.  
There’s one interesting bit here: The tests run in ‘test’ environment  
mode, and I tried running the same code in a ‘Production’ environment,  
too, and the problem totally disappears in Production environment. It  
only appears in Development environment… So maybe there’s some  
environment-related option that freezes classes, or suchlike? I should  
try to create simpler code snippets that replicate the problem. Will  
keep folks posted.  
In case anyone’s interested, the options in my development.rb file  
are :

config.cache\_classes = false  
config.whiny\_nils = true  
config.breakpoint\_server = true  
config.action\_controller.consider\_all\_requests\_local = true  
config.action\_controller.perform\_caching = false  
config.action\_view.cache\_template\_extensions = false  
config.action\_view.debug\_rjs = true

> Can you add a method\_missing to ModelT, and get these methods to work again?

Not sure how that would help… this would be like a handler to take  
action when an undefined method is called, right?

> Can you edit boot.rb, go back to 1.1.4, pass all tests, tick to 1.1.5, pass  
> all tests, and keep going until the tests break?

Working on this right now…

> Can you diff -r the two versions of ActiveRecord to see what got removed?

There are apparently loads of differences - earlier I used 1.14.4, and  
now I’m using 1.15.3, so almost every file seems different in some  
way.

Thanks,  
Sudarshan

---

<div class="post-metadata">

**Author:** ![Sudarshan\_Purohit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@Sudarshan\_Purohit](https://www.ruby-forum.com/u/Sudarshan_Purohit)\
**Post date:** [September 6, 2007, 1:52pm UTC](https://www.ruby-forum.com/t/inheriting-from-activerecord-classes/113651/4 "2007-09-06T13:52:44Z")

</div>

Hi,  
Putting down the sort-of solution, for anyone else who googles for  
"model T"s and “Rails”.  
The problem is caused due to this Rails issue :  
[http://dev.rubyonrails.org/ticket/7856](http://dev.rubyonrails.org/ticket/7856)  
[AR objects with associations behave badly after deserialization]. I  
was using Object.const\_set to save the generated classes (ModelT in  
this case), and then querying and retrieving using  
Object.const\_defined? and const\_get in a different context. After  
this, I was unable to access any of the methods and associations of  
the object class. For now, I removed this caching while in the  
Development environment.

Cheers,  
Sudarshan
