where clause select causes rollback

fugee ohu Source

I'm using nested resources resources :users do resources :addresses end

The rollback in the log below is caused by @address=Address.where(" user_id = ? and id = ? ", params[:user_id], params[:id]) in the controller

User Load (2.3ms) SELECT users.* FROM users WHERE users.id = 3 ORDER BY users.id ASC LIMIT 1 Address Load (1.5ms) SELECT addresses.* FROM addresses WHERE ( user_id = 3 and id = 5 ) (0.6ms) BEGIN (0.8ms) ROLLBACK

form-----------------------

<% if ! a.current? %>
  <% @address=Address.find(a.id) %>
  <%= form_for [@user, @address], :url => users_addresses_set_current_path(current_user.id, a.id), :method => :patch do |f| %>
  <%= f.hidden_field :current, value: true %>
  <%= f.submit 'Set as current' %>
  <% end %>

controller---------------------------

def set_current @address=Address.where(" user_id = ? and id = ? ", params[:user_id], params[:id]) @address.update(current: true) Address.where("user_id = ? and id != ?", params[:user_id], params[:id]).update_all(:current => false) respond_to do |format| format.html { redirect_to user_addresses_path(current_user.id) } format.json { head :no_content } end end

mysqlruby-on-rails

Answers

comments powered by Disqus