原创作者: hideto   阅读:1343次   评论:0条   更新时间:2011-05-26    
Rails里的Magic Column Names

当两个用户尝试同时更新同一record时,其中一个用户的更新将被覆盖,我们可以使用乐观锁来解决这个问题。

首先给Model添加一个称为lock_version的column;
# migrations/011_add_products_lock_version.rb
add_column :products, :lock_version, :integer, :default => 0, :null => false


然后修改controller:
# products_controller.rb
def update
  @product = Product.find(params[:id])
  if @product.update_attributes(params[:product])
    flash[:notice] = "Successfully updated product."
    redirect_to product_path(@product)
  else
    render :action => 'edit'
  end
rescue ActiveRecord::StaleObjectError
  @product.reload
  render :action => 'conflict'
end

更新product时,如果抛出ActiveRecord::StaleObjectError,则返回conflict页面
并且,我们在模板页面显示conflict消息、用户刚才提交的内容和最新"版本"的对象内容:
<!-- _form.rhtml -->
<%= f.hidden_field :lock_version %>

<!-- conflict.rhtml -->
Someone edited the product the same time you did. Please re-apply your changes to the product.

<h2>Your Submission:</h2>
<pre>
<% params[:product].each do |name, value| %>
  <%=h name.humanize %>: <%=h value %>
<% end %>
</pre>

<h2>Edit Product:</h2>
<% form_for :product, :url => product_path(@product), :html => { :method => :put } do |f| %>
  <%= render :partial => 'form', :locals => { :f => f } %>
  <%= submit_tag 'Resolve' %>
<% end %>

当用户发现自己提交的更新和别人有冲突时,用户可以查看到最新的对象字段,并有选择性的更改后再次提交。
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics