0

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:

enter image description here

And the model "Alerta" (alerta.rb):

class Alerta < ApplicationRecord
    has_many :alertas_clientes
    has_many :clientes, through: :alertas_clientes
end

The SQL Table:

enter image description here

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

enter image description here

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?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Ulises
  • 406
  • 7
  • 22
  • Can you show the pertinent parts of alertas_controller.rb? specifically the function that has lines 39 and 41? It looks like whatever is on line 41 is what is causing your error. If you already have those lines in the code above can you indicate which lines they are? Also you could add `logger.debug "value of foo is:#{@foo}"` in your controller before the line giving you the error and check your console log to see if it is trying to save what you think it's saving. It says you are giving an argument to a method that does not expect one. – Beartech Sep 05 '18 at 01:07
  • Thanks for the reaply @Beartech, the problem was a field name that is a reserverd word – Ulises Sep 05 '18 at 15:17

2 Answers2

1

I already know what really happend, the issue was a name field, all the relatiosn were good, but the problem was that the Model "Alerta" has a field called "send" that apparently is a reserved word of ruby. I changed to "sended" and all works fine.

I know is strange but it would be nice if Rails can show that type of error.

Ulises
  • 406
  • 7
  • 22
  • It's hard for it to know when you are using a reserved word since it just thinks you are using it the wrong way, LOL if that makes any sense. I keep this handy http://www.rubymagic.org/posts/ruby-and-rails-reserved-words – Beartech Sep 05 '18 at 15:28
  • Maybe on the migration validate all the words xD, thats a lot stuff – Ulises Sep 05 '18 at 16:24
0

Looking at the queries displayed by the error it seems that you haven't created an Alerta when calling Alerta.find(1) inside savenoti.

Assuming that you haven't created the Alerta but you have already saved the Client, you could do:

def savenoti
  begin
   @cliente = Cliente.find(6)
   alerta = Alerta.new # or a different builder if you have any info to save
   @cliente.alertas.create(alerta) //this will create and save the `AlertasCliente` instance.
  rescue => exception
      puts exception.message
      flash[:alert] = 'Error al enviar alerta.'
      redirect_to action: 'index'
  end
end

If that isn't the case, and you already saved the Alerta to the database, then include the code in lines 39 and 41 as requested by @Beartech.

In any case I would use the plain .alerts since it's clearer and that's the whole idea of declaring the through: :alertas_clientes on the model. There are more ways of creating a new record for a many_to_many association as explained here.

moondaisy
  • 4,303
  • 6
  • 41
  • 70