ruby on rails - ActiveRecord is not reloading nested object after it's updated inside a transaction -
i'm using rails 4 oracle 12c , need update status of user, , use new status in validation model need update:
class user has_many :posts def custom_update!(new_status) relevant_posts = user.posts.active_or_something activerecord::base.transaction update!(status: new_status) relevant_posts.each { |post| post.update_stuff! } end end end class post belongs_to :user validate :pesky_validation def update_stuff! # can call other places, need transaction here activerecord::base.transaction update!(some_stuff: 'some value') end end def pesky_validation if user.status == old_status errors.add(:base, 'nope') end end end
however, failing , receive validation error pesky_validation
, because user inside post doesn't have updated status.
the problem is, when first update user, instantiated users inside relevant_posts
variable not yet updated, , i'd need fix call reload
, however, maybe because i'm inside transaction, not working, , pesky_validation
failing.
relevant_users.first.user.reload
, example, reloads user same old status had before update, , i'm assuming it's because transaction not yet committed. how can solve , update references new status?
Comments
Post a Comment