In my ruby on Rails 4 app, a user clicks on a link on a Deal page (see above the code. link is 'view infos of the latest Opportunity').
Once he clicks the link on the page, an ajax call is then made to show him some info about one of latest Opportunity via Rails UJS (link remote=> true).
I would like to update the 'opportunity_status' attribute of the Opportunity row that is read as SOON as POSSIBLE after the row has been read by postgresql. but how to know the row has been "read"?
Is there a reliable way to know the postgresql query has been done, i.e the READ query on the table/row has been performed and finished, and then if the READ query has been performed then straight away trigger an update on the 'opportunity status' column changing it from 'available' to 'not available anymore' of the row that has been read inside the READ query?
I'm quite rookie on postgresql so I don't know how to do this.
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
Here is the modal trigger via Ajax: views/deals/opportunity_modal. Note here how I tried here to pass Deal but without success so that the line
$('body').append('<%= j render partial: "deals/deal_opportunities_modal" %>');
$('#myModal').modal('show');
/app/views/deals/_deal_opportunities_modal.html.erb
Here is the modal view/content now:
<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>
@opportunity comes from this controller
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
So inside the modal, the latest opportunity is shown. It comes form this controller's @shown_opportunity = Opportunity.where('deal_id = ? AND opportunity_status = ?', @deal.id, 'available').first
I want to update the attribute 'opportunity_status' of this opportunity as SOON as possible, but reliably, when this query @latest_opportunity = Opportunity.where('deal_id = ? AND opportunity_status = ?', @deal.id, true).first had been COMPLETED inside the Opportunity table (it's a read, I need to know when the READ is finished).
How to do that?
Aucun commentaire:
Enregistrer un commentaire