ruby on rails - Cascade of deletes ActiveRecord -
how can add cascade of deletes remove profile, todolist, , todoitem rows user removed.
user model:
class user < activerecord::base has_one :profile has_many :todo_lists has_many :todo_items, through: :todo_lists, source: :todo_items validates :username, presence: true end
profile model:
class profile < activerecord::base belongs_to :user validates :first_name, presence: true validates :last_name, presence: true validates :gender, inclusion: %w(male female) validate :first_and_last validate :male_sue def first_and_last if (first_name.nil? , last_name.nil?) errors.add(:base, "specify first or last.") end end def male_sue if (first_name == "sue" , gender == "male") errors.add(:base, "we prevent male name sue.") end end end
todolist model:
class todolist < activerecord::base belongs_to :user has_many :todo_items, dependent: :destroy default_scope { order :list_due_date } end
todoitem model:
class todoitem < activerecord::base belongs_to :todo_list default_scope {order :due_date } end
thanks, michael.
i guess adding dependent: :destroy
do.
#user.rb class user < activerecord::base has_one :profile, dependent: :destroy has_many :todo_lists, dependent: :destroy has_many :todo_items, through: :todo_lists, source: :todo_items, dependent: :destroy validates :username, presence: true end
Comments
Post a Comment