Rails単一継承(STI)とサブdir構成を実現する方法
方法1
app/models/parent.rb
app/models/parent/mother.rb
app/models/parent/father.rb
class Parent < ApplicationRecord; end
class Parent::Mother < Parent; end
class Parent::Father < Parent; end
# rails consoleで検索するには
Parent::Mother.all
Parent.where(type: 'Parent::Mother')
方法2
# add this to config/application.rb
config.autoload_paths += Dir[Rails.root.join('app', 'models', 'parent')]
app/models/parent.rb
app/models/parent/mother.rb
app/models/parent/father.rb
class Parent < ApplicationRecord; end
class Mother < Parent; end
class Father < Parent; end
# rails consoleで検索するには
Mother.all
Mother.where()