Когда я использую update_attributes, он должен удалять определенные записи и удалять их зависимых (как я использую зависимый:: уничтожить). Однако я получаю ошибку внешнего ключа и не знаю, что делать по-другому.
У меня есть следующее
class StudentGoal < ApplicationRecord
belongs_to :goal
belongs_to :student
has_many :student_goal_scores, dependent: :destroy
end
class StudentGoalScore < ApplicationRecord
belongs_to :student_goal
end
class Goal < ApplicationRecord
has_many :student_goals, dependent: :destroy
has_many :students, through: :student_goals
end
В консоли rails, если я запускаю следующее (ранее у цели было несколько учеников):
Goal.find(2).update_attributes(students: [Student.find(69)])
Я получаю следующую ошибку
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR:
update or delete on table "student_goals" violates foreign key constraint
"fk_rails_bcae4e177a" on table "student_goal_scores") DETAIL: Key (id)=(49) is
still referenced from table "student_goal_scores".
Если я просто попытаюсь уничтожить StudentGoal с StudentGoal.find (number) .destroy, StudentGoalScores будут удалены правильно.
Что я могу сделать, чтобы update_attributes правильно каскадно удалял?