Inheriting from Activerecord classes

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

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/
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax

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

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
[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