I have a pretty typical Order model that has_many Lines
class Order < ActiveRecord::Base has_many :lines validates_associated :lines
Once the order is completed, it should not be possible to change any attributes or related lines (although you can change the status that is not completed).
validate do if completed_at.nil? == false && completed_at_was.nil? == false errors.add(:base, "You can't change once complete") end end
This works fine, but if you add, delete, or modify related Lines , it doesnβt interfere.
In my Line model, I have the following check:
validate do if order && order.completed_at.nil? == false errors.add(:base, "Cannot change once order completed.") end end
This successfully stops the rows in the completed order, which is changing, and prevents the row from being added to the completed order.
Therefore, I need to also prevent the removal of rows from a completed order. I tried this in the Line model:
validate do if order_id_was.nil? == false if Order.find(order_id_was).completed_at.nil? == false errors.add(:base, "Cannot change once order completed.") end end end
This works great to prevent the Line from Ordering when changing the Line directly. However, when you edit the Order and delete the Line , the check is not performed because it has already been deleted from the Order .. p>
So ... In short, how can I verify that the lines associated with an order are not changed or added or deleted?
I think I am missing something obvious.