4
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

関連するモデルも一緒に削除する

4
Last updated at Posted at 2014-05-31
# 会社モデル
class Company << ActiveRecord::Base
  #dependentがキモ
  #↑ちなみに”依存している”という意味らしい
  has_many :members, dependent: :destroy
end
# 社員モデル
class Member << ActiveRecord::Base
  belongs_to :company
end

ER図風AAだとこんな感じ
1対多の関係
company --< members

このように記述しておけば、Companyモデルを削除する際に関連するMemberモデルも一緒に削除される。

会社が倒産したら社員も存在しなくなるのである…

ググるワード
rails association dependent

4
9
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
4
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?