μ€λ μ¬μ ν μ‘΄μ¬νλ λ¬Έμ #868μ λ€μ μ½λλ€.
λλ ruby-1.9.3-p362μ rails 3.2.9λ₯Ό μ¬μ©νλ€
μ μΌν ν΄κ²°μ±
μ μμ ν΄λμ€λ₯Ό μꡬνκ±°λ 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">]
λ μΌ 4μμ μ¬ννμ΅λλ€.
λ μΌμ€ 4.0.0.λ² ν
λ£¨λΉ 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 []>
μ΄κ²μ ν΄λμ€μ μ§μ° λ‘λμ ν¨κ» κ°λ° λͺ¨λμμ STIλ₯Ό μ¬μ©νλ μλ €μ§ μ ν μ¬νμ
λλ€. Railsκ° μ΄λ¬ν νμ ν΄λμ€λ₯Ό μΈμνλλ‘ νλ €λ©΄ require_dependency
λ₯Ό μ¬μ©ν΄μΌ ν©λλ€. μ:
# app/models/support.rb
class Support < User
end
require_dependency 'administrator'
require_dependency 'employee'
μ΄λ¬ν νμ ν΄λμ€κ° λ§μ κ²½μ° Dir[]
λ° app
μ νΉμ ν΄λλ₯Ό μ¬μ©νμ¬ λͺ¨λ λ‘λν μ μμ΅λλ€. μ:
# app/models/user.rb
class User < ActiveRecord::Base
end
Dir["#{Rails.root}/app/users/*.rb"].each do |file|
require_dependency file
end
λλ μΈ λ²μ§Έ λμμ Model.descendants
λͺ©λ‘μ νλ μ½λ©νλ κ²μ
λλ€. μ:
# app/models/user.rb
class User < ActiveRecord::Base
def self.descendants
[Support, Administrator, Employee]
end
end
λλ λ§μ§λ§ κ²μ κ°μΈμ μΌλ‘ μ¬μ©νμ§ μμμ§λ§ μλν΄μΌν©λλ€.
λλ μ΄λ° μ’
λ₯μ λ¬Έμ κ° μκ³ κ·Έκ²μ μλμν¬ μ μμ΅λλ€. λ΄ λͺ¨λΈμ A, B(λ° κΈ°ν) < C, D(λ° κΈ°ν) < Bμ
λλ€.
μ½λ λ³κ²½ ν "μ΄κΈ°νλμ§ μμ μμ B"κ° νμλ©λλ€. λͺ κ°μ§ λ°©λ²μΌλ‘ require_dependencyλ₯Ό μλνμ§λ§ 1) μ΄λ€ ν΄λμ€(λ€)μ΄ νμνμ§(B?), 2) μ΄λμ ν΄μΌ νλμ§ λͺ¨λ¦
λλ€.
μ΄λ¬ν λͺ¨λΈμ μ¬λ¬ κ°μ§ λ°©μμΌλ‘ λ€λ₯Έ λͺ¨λΈκ³Ό μ°κ²°λλ©°, μ΄λ λ‘λ μμμλ μν₯μ λ―ΈμΉ μ μμ΅λλ€(μ: Aκ° Xμ, Xμ B λ° Cκ° λ§μ΄ μμ).
κ°μ₯ μ μ©ν λκΈ
μ΄κ²μ ν΄λμ€μ μ§μ° λ‘λμ ν¨κ» κ°λ° λͺ¨λμμ STIλ₯Ό μ¬μ©νλ μλ €μ§ μ ν μ¬νμ λλ€. Railsκ° μ΄λ¬ν νμ ν΄λμ€λ₯Ό μΈμνλλ‘ νλ €λ©΄
require_dependency
λ₯Ό μ¬μ©ν΄μΌ ν©λλ€. μ:μ΄λ¬ν νμ ν΄λμ€κ° λ§μ κ²½μ°
Dir[]
λ°app
μ νΉμ ν΄λλ₯Ό μ¬μ©νμ¬ λͺ¨λ λ‘λν μ μμ΅λλ€. μ:λλ μΈ λ²μ§Έ λμμ
Model.descendants
λͺ©λ‘μ νλ μ½λ©νλ κ²μ λλ€. μ:λλ λ§μ§λ§ κ²μ κ°μΈμ μΌλ‘ μ¬μ©νμ§ μμμ§λ§ μλν΄μΌν©λλ€.