I have a model Todo
with self referential parents and children
has_and_belongs_to_many :children, class_name: "Todo", join_table: "todos_todos", foreign_key: "parent_id", association_foreign_key: :child_id
has_and_belongs_to_many :parents, class_name: "Todo", join_table: "todos_todos", foreign_key: "child_id", association_foreign_key: :parent_id
I want do something like before_add
on both of them that will throw a validation error. I've tried before_add: [:parent_due_after_or_on_self]
with
def parent_due_after_or_on_self(parent)
if parent.due < self.due
self.errors.add(:parents, "Parent cannot be due before child")
end
end
But that does nothing. I've tried adding raise "Parent cannot be due before child"
right after adding the error, and that gets me the error message, but now I can't rescue it to return the user to the form to fix their mistake. I'm positive I got this working earlier this week, but I've lost the code and can't remember what it was exactly.