I am trying to implement the Optimistic Locking for Race Condition. For that, I added an extra column lock_version
in the Product: Model through migration. And passing to through attr_accessible :lock_version
. I am able to update model through the rails console, however, through View I am not able to update the model.
Product: Model's new field:
# lock_version :integer(4) default(0), not null
Passing lock_version in Product: Model:
attr_accessible :lock_version
Pass it as hidden_field in View/edit:
form.hidden_field :lock_version
Product: Model Controller:
def update
...
@product.update_with_conflict_validation(params[:car])
end
Inside Product: Model:
def update_with_conflict_validation(*args)
update_attributes(*args)
rescue ActiveRecord::StaleObjectError
errors.add :base, "This record changed while you are editing."
false
end
I am using Ruby Version
Loading development environment (Rails 3.2.22.4)
irb(main):001:0> RUBY_VERSION
=> "1.9.3"
Please let me know why I am not able to update model through UI/View, however, able to update through rails console.
Reference: https://www.youtube.com/watch?v=dcfP37d8-ZI
Aucun commentaire:
Enregistrer un commentaire