Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STI, inheritance and reload! bug in development environment #8699

Closed
tgautier opened this issue Jan 2, 2013 · 3 comments
Closed

STI, inheritance and reload! bug in development environment #8699

tgautier opened this issue Jan 2, 2013 · 3 comments

Comments

@tgautier
Copy link

tgautier commented Jan 2, 2013

I'm reopening issue #868 as it it still present today.

I've used ruby-1.9.3-p362 and rails 3.2.9

The only solution is to require child classes or to use config.cache_classes = true

> Support.all
 => [] 
> Employee
 => Employee(id: integer, type: string) 
> Administrator
 => Administrator(id: integer, type: string) 
> Support.all
 => [#<Administrator id: 1, type: "Administrator">, #<Employee id: 2, type: "Employee">] 
@vairix-pmonfort
Copy link

I've reproduce it on rails 4

Rails 4.0.0.beta
Ruby 1.9.3p194

1.9.3p194 :001 > Administrator.create

1.9.3p194 :003 > Employee.create

1.9.3p194 :008 > Support.all
  Support Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."type" IN ('Support', 'Administrator', 'Employee')
 => #<ActiveRecord::Relation [#<Administrator id: 1, type: "Administrator">, #<Employee id: 2, type: "Employee">]> 

1.9.3p194 :009 > reload!
Reloading...
 => true 

1.9.3p194 :010 > Support.all
  Support Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."type" IN ('Support')
 => #<ActiveRecord::Relation []> 

@pixeltrix
Copy link
Contributor

This is known limitation of using STI in development mode with lazy loading of classes. You need to use require_dependency to make Rails aware of those subclasses, e.g:

# app/models/support.rb
class Support < User
end

require_dependency 'administrator'
require_dependency 'employee'

If you have a lot of these subclasses then you can use Dir[] and a special folder in app to load them all, e.g:

# app/models/user.rb
class User < ActiveRecord::Base
end

Dir["#{Rails.root}/app/users/*.rb"].each do |file|
  require_dependency file
end

Or a third alternative is to hard code the list in Model.descendants, e.g:

# app/models/user.rb
class User < ActiveRecord::Base
  def self.descendants
    [Support, Administrator, Employee]
  end
end

I haven't used the last one personally but it should work.

@stanley90
Copy link

I have this kind of problem and can't get it working. My models are A, B (and more) < C, D (and more) < B.
After a code change, I get "uninitialized constant B". Tried require_dependency in a couple of ways, but don't really know 1) which class(es) to require (B?), and 2) where to do it.
These models are associated with other models in several ways, this might have an impact on the load order too (e.g. A belongs_to X, X has_many Bs and Cs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants