Rails 4/postgresql - 在对行执行读取查询后更新模型属性
Rails 4/postgresql - Update model attribute AFTER Read query has been performed on the row
在我的 ruby Rails 4 应用程序中,用户点击交易页面上的 link(见上面的代码。link 是 'view infos of the latest Opportunity').
一旦他点击页面上的 link,就会进行 ajax 调用,通过 Rails UJS(link 远程=> 真)。
我想更新 Opportunity 行的 'opportunity_status' 属性,该行在 postgresql 读取后尽快读取。但如何知道该行已 "read"?
是否有可靠的方法知道 postgresql 查询已完成,即 table/row 上的 READ 查询已执行并完成,然后如果已执行 READ 查询,则立即 触发对 'opportunity status' 列的更新,将其从 'available' 更改为已在 READ 查询中读取的行的 'not available anymore' ?
我是 postgresql 的菜鸟,所以我不知道该怎么做。
controller/deals_controller.rb
def showcase
@deal = Deal.friendly.find(params[:id])
respond_to do |format|
format.html # showcase.html.erb
format.json { render json: @deal }
end
end
def show_opportunities
@deal = Deal.friendly.find(params[:id])
@opportunity = Opportunity.where('deal_id = ? AND deal_type = ?',
@deal.id, "high tech").first
respond_to do |format|
format.js
end
end
app/views/deals/showcase.html.erb
<% if @deal.present? %>
this is the beginning
<%= render 'deals/deal_info_zone' %>
this is the end
<% end %>
views/deals/_deal_info_zone.html.erb
<div id="zoneA">
<div style="color:red;padding-top: 150px;">
<%= link_to "view infos of the latest Opportunity", deal_opportunities_modal_path, remote: true %>
</div>
</div>
views/deals/deal_opportunities_modal.js.erb
这是通过 Ajax 的模态触发器:views/deals/opportunity_modal。请注意我在这里是如何尝试通过 Deal 但没有成功的,因此
行
$('body').append('<%= j render partial: "deals/deal_opportunities_modal" %>');
$('#myModal').modal('show');
/app/views/deals/_deal_opportunities_modal.html.erb
现在是模态view/content:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
this the the latest opportunity: <%= @latest_opportunity.name %> <br/>
</div>
</div>
</div>
</div>
@机会来自这个控制器
def deal_opportunities_modal
@deal = Deal.friendly.find(params[:id])
@latest_opportunity = Opportunity.where('deal_id = ? AND opportunity_status = ?', @deal.id, 'available').first
respond_to do |format|
format.js
end
end
所以在模态中,显示了最新机会。它来自这个控制器的
@shown_opportunity = Opportunity.where('deal_id = ? AND opportunity_status = ?', @deal.id, 'available').first
我想尽快更新此机会的属性 'opportunity_status',但可靠地,当此查询 @latest_opportunity = Opportunity.where('deal_id = ? AND opportunity_status = ?', @ deal.id, true).first had been COMPLETED inside the Opportunity table (这是一个读取,我需要知道读取何时完成)。
怎么做?
您可以在您的 show_opportunities
方法中直接在控制器中设置和保存新状态,在读取记录之后,但在将数据发送给用户之前。
如果你想确保同时没有其他人获得同样的机会,你需要在读取时锁定数据库行。参见“pessimistic locking”
在我的 ruby Rails 4 应用程序中,用户点击交易页面上的 link(见上面的代码。link 是 'view infos of the latest Opportunity').
一旦他点击页面上的 link,就会进行 ajax 调用,通过 Rails UJS(link 远程=> 真)。
我想更新 Opportunity 行的 'opportunity_status' 属性,该行在 postgresql 读取后尽快读取。但如何知道该行已 "read"?
是否有可靠的方法知道 postgresql 查询已完成,即 table/row 上的 READ 查询已执行并完成,然后如果已执行 READ 查询,则立即 触发对 'opportunity status' 列的更新,将其从 'available' 更改为已在 READ 查询中读取的行的 'not available anymore' ?
我是 postgresql 的菜鸟,所以我不知道该怎么做。
controller/deals_controller.rb
def showcase
@deal = Deal.friendly.find(params[:id])
respond_to do |format|
format.html # showcase.html.erb
format.json { render json: @deal }
end
end
def show_opportunities
@deal = Deal.friendly.find(params[:id])
@opportunity = Opportunity.where('deal_id = ? AND deal_type = ?',
@deal.id, "high tech").first
respond_to do |format|
format.js
end
end
app/views/deals/showcase.html.erb
<% if @deal.present? %>
this is the beginning
<%= render 'deals/deal_info_zone' %>
this is the end
<% end %>
views/deals/_deal_info_zone.html.erb
<div id="zoneA">
<div style="color:red;padding-top: 150px;">
<%= link_to "view infos of the latest Opportunity", deal_opportunities_modal_path, remote: true %>
</div>
</div>
views/deals/deal_opportunities_modal.js.erb
这是通过 Ajax 的模态触发器:views/deals/opportunity_modal。请注意我在这里是如何尝试通过 Deal 但没有成功的,因此
行$('body').append('<%= j render partial: "deals/deal_opportunities_modal" %>');
$('#myModal').modal('show');
/app/views/deals/_deal_opportunities_modal.html.erb
现在是模态view/content:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
this the the latest opportunity: <%= @latest_opportunity.name %> <br/>
</div>
</div>
</div>
</div>
@机会来自这个控制器
def deal_opportunities_modal
@deal = Deal.friendly.find(params[:id])
@latest_opportunity = Opportunity.where('deal_id = ? AND opportunity_status = ?', @deal.id, 'available').first
respond_to do |format|
format.js
end
end
所以在模态中,显示了最新机会。它来自这个控制器的 @shown_opportunity = Opportunity.where('deal_id = ? AND opportunity_status = ?', @deal.id, 'available').first
我想尽快更新此机会的属性 'opportunity_status',但可靠地,当此查询 @latest_opportunity = Opportunity.where('deal_id = ? AND opportunity_status = ?', @ deal.id, true).first had been COMPLETED inside the Opportunity table (这是一个读取,我需要知道读取何时完成)。
怎么做?
您可以在您的 show_opportunities
方法中直接在控制器中设置和保存新状态,在读取记录之后,但在将数据发送给用户之前。
如果你想确保同时没有其他人获得同样的机会,你需要在读取时锁定数据库行。参见“pessimistic locking”