I got weird problem. I just deployed my Rails application on Heroku. But seems something wrong with toggle function that change boolean data from 0 to 1 on PostgreSQL.
Ruby 2.4.5
Rails 4.2.8
Heroku
PostgreSQL(production)
MySQL(development)
What I expect is that every time user put the check mark or off on checkbox, data corresponded column should be changed 1 to 0 or 0 to 1.
I tried to use 'true' instead of '1' and 'TrueClass' and so on. But all of them didn't work.
Here's the processing flow from user put a check mark or off in checkbox on _item.html.erb to Heroku database (postgreSQL).
_item.html.erb
<%= check_box_tag '', '', item.check, {'data-id' => item.id, 'data-user-id' => item.user_id ,class: "check_#{item.id} option-input"} %>
javascript
$(document).on('turbolinks:load', function() {
$("input[type=checkbox]").click(function(){
$.post('/items/'+$(this).data('id')+'/toggle');
});
});
items_controller.rb
def toggle
render nothing: true
@item = Item.find(params[:id])
#I just changed here to some other pattern
@item.check = !@item.check
@item.save
#didn't work
if @item.check.is_a?(FalseClass)
@item.check = '1'
else
@item.check = '0'
end
@item.save
#didn't work
if @item.check.is_a?(FalseClass)
@item.check = true
else
@item.check = false
end
@item.save
#didn't work
if @item.check.is_a?(FalseClass)
@item.check = (TrueClass)
else
@item.check = (FalseClass)
end
@item.save
#didn't work
if @item.check.is_a?(FalseClass)
@item.check = 1
else
@item.check = 0
end
@item.save
end
Heroku logs
2019-04-17T00:30:51.155800+00:00 heroku[router]: at=info method=POST path="/items/8/toggle" host=********* request_id=0403023c-56f5-4933-82af-be33b21dd21e fwd="130.212.95.200" dyno=web.1 connect=0ms service=25ms status=200 bytes=880 protocol=https
2019-04-17T00:30:51.137644+00:00 app[web.1]: Started POST "/items/8/toggle" for 130.212.95.200 at 2019-04-17 00:30:51 +0000
2019-04-17T00:30:51.141054+00:00 app[web.1]: Processing by ItemsController#toggle as */*
2019-04-17T00:30:51.141085+00:00 app[web.1]: Parameters: {"id"=>"8"}
2019-04-17T00:30:51.145264+00:00 app[web.1]: [1m[36mUser Load (1.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1[0m [["id", 2]]
2019-04-17T00:30:51.146978+00:00 app[web.1]: Rendered text template (0.0ms)
2019-04-17T00:30:51.149372+00:00 app[web.1]: [1m[35mItem Load (1.3ms)[0m SELECT "items".* FROM "items" WHERE "items"."deleted_at" IS NULL AND "items"."id" = $1 LIMIT 1 [["id", 8]]
2019-04-17T00:30:51.151213+00:00 app[web.1]: [1m[36m (1.0ms)[0m [1mBEGIN[0m
2019-04-17T00:30:51.153240+00:00 app[web.1]: [1m[35m (1.0ms)[0m COMMIT
2019-04-17T00:30:51.153486+00:00 app[web.1]: Completed 200 OK in 11ms (Views: 0.4ms | ActiveRecord: 4.5ms)
I expect the toggle function works correctly.
This error doesn't show up on development environment. So, it seems that the something difference between MySQL and PostgreSQL causes it.