Reputation: 2881
Pretty basic question:
I have a model x that has_many y, and model y belong_to x.
If I lock an instance of model x with id x_id, does it also lock the associated rows in the table for model y which has has value x_id under the join column?
Or does ror locking just lock the active record and does not care about its associations?
Thanks!
Upvotes: 5
Views: 3790
Reputation: 48616
From what i know, it would not lock any associations. It just locks rows, without caring for model associations.
Upvotes: 1
Reputation: 19738
It seems as if there are two locking strategies in rails, optimistic (which doesn't actually lock rows but ActiveRecord raises ActiveRecord::StaleObjectError for multiple updates to the same row [except the first update, which will succeed]), and pessimistic (which appends FOR UPDATE
to the select statement and actually locks the rows (assuming your database supports locking). None of the ActiveRecord Locking documentation I read through implies that there is any magic that causes/allows associative records to be locked.
Since you can pass your own locking clause, I would suggest on reading up on how your specific database handles the clause rails uses for pessimistic locking (select ... for update
) and other clauses that you can pass (Using ActiveRecord#lock!).
Upvotes: 0