Do rubies on rails automatically block active record associations when blocking an active record?

Pretty simple question:

I have a model x that has_many y, and model y belongs to x.

If I lock an instance of model x with id x_id, will it also lock the related rows in the table for model y, which has the value x_id in the join column?

Or does it block only the active record and does not care about its relationships?

Thanks!

+4
source share
2 answers

From what I know, he will not block any associations. It simply blocks the lines without worrying about model associations.

+1
source

It seems that two locking strategies in rails are optimistic (which actually don't block rows, but ActiveRecord activates ActiveRecord :: StaleObjectError for multiple updates of the same row [except for the first update to be executed]) and pessimistic (which adds FOR UPDATE to select statement and actually blocks the line (assuming that your database supports lock). None of the documentation to block the ActiveRecord, I'm reading through it implies that there is any magic that is / are blocked by allowing amb association records.

Since you can submit your own lock proposal, I would advise you to read about how your particular database handles the resulting pessimistic lock offers ( select ... for update ) and other offers that you can pass (using ActiveRecord # lock! )

0
source

All Articles