简体   繁体   中英

does ruby on rails automatically lock active record associations when locking the active record?

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!

From what i know, it would not lock any associations. It just locks rows, without caring for model associations.

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! ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM