From 3171ca7177b7ab632d69b6492f3c6abd793dc48b Mon Sep 17 00:00:00 2001 From: f Date: Fri, 12 Jul 2019 14:11:24 -0300 Subject: [PATCH] serializar booleanos en sqlite3 --- config/application.rb | 15 +++++++---- db/migrate/20190703200455_create_sitios.rb | 9 +++++-- db/migrate/20190712165059_sqlite_boolean.rb | 28 +++++++++++++++++++++ db/schema.rb | 2 +- 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20190712165059_sqlite_boolean.rb diff --git a/config/application.rb b/config/application.rb index 2cc92e8..579177e 100644 --- a/config/application.rb +++ b/config/application.rb @@ -19,13 +19,18 @@ require 'rails/test_unit/railtie' Bundler.require(*Rails.groups) module Sutty + # Sutty! class Application < Rails::Application - # Initialize configuration defaults for originally generated Rails version. + # Initialize configuration defaults for originally generated Rails + # version. config.load_defaults 5.1 - # Settings in config/environments/* take precedence over those specified here. - # Application configuration should go into files in config/initializers - # -- all .rb files in that directory are automatically loaded. - config.action_dispatch.rescue_responses['Pundit::NotAuthorizedError'] = :forbidden + # Settings in config/environments/* take precedence over those + # specified here. Application configuration should go into files in + # config/initializers -- all .rb files in that directory are + # automatically loaded. + config.action_dispatch + .rescue_responses['Pundit::NotAuthorizedError'] = :forbidden + config.active_record.sqlite3.represent_boolean_as_integer = true end end diff --git a/db/migrate/20190703200455_create_sitios.rb b/db/migrate/20190703200455_create_sitios.rb index c8e45d3..7f8ae8f 100644 --- a/db/migrate/20190703200455_create_sitios.rb +++ b/db/migrate/20190703200455_create_sitios.rb @@ -44,7 +44,10 @@ class CreateSitios < ActiveRecord::Migration[5.2] usuarie ||= Usuarie.create(email: email, password: SecureRandom.hex, confirmed_at: Date.today) - site.usuaries << usuarie + + sql = "insert into sites_usuaries (site_id, usuarie_id) + values (#{site.id}, #{usuarie.id});" + ActiveRecord::Base.connection.execute(sql) end invitadxs.each do |email| @@ -52,7 +55,9 @@ class CreateSitios < ActiveRecord::Migration[5.2] usuarie ||= Usuarie.create(email: email, password: SecureRandom.hex, confirmed_at: Date.today) - site.invitades << usuarie + sql = "insert into invitades_sites (site_id, usuarie_id) + values (#{site.id}, #{usuarie.id});" + ActiveRecord::Base.connection.execute(sql) end end end diff --git a/db/migrate/20190712165059_sqlite_boolean.rb b/db/migrate/20190712165059_sqlite_boolean.rb new file mode 100644 index 0000000..7f41afd --- /dev/null +++ b/db/migrate/20190712165059_sqlite_boolean.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# Convertir los valores binarios de sqlite +class SqliteBoolean < ActiveRecord::Migration[5.2] + def up + return unless adapter_name == 'SQLite' + + Usuarie.where("acepta_politicas_de_privacidad = 't'") + .update_all(acepta_politicas_de_privacidad: 1) + Usuarie.where("acepta_politicas_de_privacidad = 'f'") + .update_all(acepta_politicas_de_privacidad: 0) + + change_column :usuaries, :acepta_politicas_de_privacidad, :boolean, + default: 0 + end + + def down + return unless adapter_name == 'SQLite' + + Usuarie.where('acepta_politicas_de_privacidad = 1') + .update_all(acepta_politicas_de_privacidad: 't') + Usuarie.where('acepta_politicas_de_privacidad = 0') + .update_all(acepta_politicas_de_privacidad: 'f') + + change_column :usuaries, :acepta_politicas_de_privacidad, :boolean, + default: 'f' + end +end diff --git a/db/schema.rb b/db/schema.rb index c01df6a..98e398c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,7 +12,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20_190_711_183_726) do +ActiveRecord::Schema.define(version: 20_190_712_165_059) do create_table 'roles', force: :cascade do |t| t.datetime 'created_at', null: false t.datetime 'updated_at', null: false