We have this model called "Cliente" (cliente.rb):
class Cliente < ApplicationRecord
has_many :clientes_hardwares
has_many :alertas_clientes
has_many :sucursales
has_many :alertas, through: :alertas_clientes
has_many :hardwares, through: :clientes_hardwares
end
The SQL table:
And the model "Alerta" (alerta.rb):
class Alerta < ApplicationRecord
has_many :alertas_clientes
has_many :clientes, through: :alertas_clientes
end
The SQL Table:
And after that we created a join table.
class CreateJoinTableClientesAlertas < ActiveRecord::Migration[5.2]
def change
create_join_table :clientes, :alertas do |t|
# t.index [:cliente_id, :alerta_id]
# t.index [:alerta_id, :cliente_id]
end
end
end
The SQL table is called "alertas_clientes" and his structure very simple
The model is file (alertas_cliente.rb):
class AlertasCliente < ApplicationRecord
belongs_to :cliente
belongs_to :alerta
end
We want to save the relation on the table but the console doesn't show the actual error.
def savenoti
begin
@cliente = Cliente.find(6)
@cliente.alertas_clientes.build(
:alerta => Alerta.find(1)
)
@cliente.save
rescue => exception
puts exception.message
flash[:alert] = 'Error al enviar alerta.'
redirect_to action: 'index'
end
end
But the console shows:
Processing by AlertasController#sendnoti as HTML
Cliente Load (0.3ms) SELECT `clientes`.* FROM `clientes` WHERE `clientes`.`id` = 6 LIMIT 1
↳ app/controllers/alertas_controller.rb:37
Alerta Load (0.2ms) SELECT `alerta`.* FROM `alerta` WHERE `alerta`.`id` = 1 LIMIT 1
↳ app/controllers/alertas_controller.rb:39
(0.1ms) BEGIN
↳ app/controllers/alertas_controller.rb:41
(0.1ms) ROLLBACK
↳ app/controllers/alertas_controller.rb:41
wrong number of arguments (given 1, expected 0)
Am I missing something?