ActiveRecordのインプットメモ
belongs_to
デフォでpresence:trueになっている
ネームスペース済みのモデルを作成する
https://qiita.com/takaesu_ug/items/198dbe7e509b9bf94395
コマンド
rails g model Hoge::User
テーブルのプレフィックスを指定
code:app/models/hoge.rb
module Hoge
def self.table_name_prefix
'hoge_' #DBのtable名にadmin_というプレフィックスがあることが前提
end
end
モデル
code: app/models/hoge/user.rb
module Hoge
class User < ApplicationRecord
end
end
外部キー(対象がネームスペースつきの時は、classNameを指定する必要あり)
code:user,rb
class User < ApplicationRecord
with_options dependent: :restrict_with_error,
inverse_of: :user do
has_one :hoge_user, class_name: 'Hoge::User'
end
end
code:hoge/user.rb
module Hoge
class User < ApplicationRecord
belongs_to :user
end
end
単一テーブル継承(STI)
https://railsguides.jp/association_basics.html#単一テーブル継承(sti)
deligate_type
https://railsguides.jp/association_basics.html#delegated-types