Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoR: MyModel.descendants returns [] in a view after the first call?

I want to display a selection list of MyModel subclasses in a view. It's not working yet, so for sanity checking, I included this in my view:

<%= MyModel.descendants %>

The first time I render this page after re-starting the server, it shows the list of descendants (there are six). All subsequent times, it shows up as an empty list [].

FWIW, I have a require statement in my initializers:

Dir[Rails.root.join("app/models/my_models/**/*.rb").to_s].each {|f| require f}

... and I've verified that they're getting required.

What the @($%& is going on?

like image 775
fearless_fool Avatar asked May 04 '11 18:05

fearless_fool


1 Answers

I had the same problem. Solved it by adding a config/initializers/preload_models.rb with:

Dir[Rails.root + 'app/models/*.rb'].map {|f| File.basename(f, '.*').camelize.constantize }

Hope that helps somebody.

like image 184
behe Avatar answered Oct 17 '22 03:10

behe