LoginSignup
0
1

More than 5 years have passed since last update.

Rails単一継承(STI)とサブdir構成を実現する方法

Posted at

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()

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1