From d16947d1babc7c073085682d080bba56411af58f Mon Sep 17 00:00:00 2001 From: f Date: Sat, 11 Jun 2022 14:48:31 -0300 Subject: [PATCH] los sensores tienen lecturas --- app/controllers/readings_controller.rb | 4 ++-- app/models/arduino.rb | 1 - app/models/reading.rb | 3 +-- ...0611174308_remove_reading_to_arduino_association.rb | 10 ++++++++++ 4 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20220611174308_remove_reading_to_arduino_association.rb diff --git a/app/controllers/readings_controller.rb b/app/controllers/readings_controller.rb index 44e53b8..058d701 100644 --- a/app/controllers/readings_controller.rb +++ b/app/controllers/readings_controller.rb @@ -21,10 +21,10 @@ class ReadingsController < ActionController::API params[:arduinos]&.reject do |a| a[:id].blank? || a[:sensores].empty? end&.each do |a| - arduino = reading.arduinos.find_or_initialize_by(local_id: a[:id]) + arduino = raspberry.arduinos.find_or_initialize_by(local_id: a[:id]) a[:sensores].each do |s| - arduino.sensors.build(sensor_params s) + arduino.sensors.build(reading: reading, **sensor_params(s)) end end diff --git a/app/models/arduino.rb b/app/models/arduino.rb index 29046d3..73093bd 100644 --- a/app/models/arduino.rb +++ b/app/models/arduino.rb @@ -2,7 +2,6 @@ class Arduino < ApplicationRecord belongs_to :raspberry - belongs_to :reading has_many :sensors validates_presence_of :local_id diff --git a/app/models/reading.rb b/app/models/reading.rb index addf084..3ed53ea 100644 --- a/app/models/reading.rb +++ b/app/models/reading.rb @@ -2,8 +2,7 @@ class Reading < ApplicationRecord belongs_to :raspberry - has_many :arduinos - has_many :sensors, through: :arduinos + has_many :sensors # @param :public_key [SSHData::PublicKey] def verify(public_key) diff --git a/db/migrate/20220611174308_remove_reading_to_arduino_association.rb b/db/migrate/20220611174308_remove_reading_to_arduino_association.rb new file mode 100644 index 0000000..69f489d --- /dev/null +++ b/db/migrate/20220611174308_remove_reading_to_arduino_association.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +# XXX: La migraciĆ³n que deduplicaba Arduinos pierde la relaciĆ³n entre +# lectura y sensado. +class RemoveReadingToArduinoAssociation < ActiveRecord::Migration[6.1] + def change + add_belongs_to :sensors, :reading, index: true + remove_belongs_to :arduinos, :reading, index: true + end +end