All threads / Ruby on Rails save on model has_many to has_many doesn't trigger on build

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Ruby on Rails save on model has_many to has_many doesn't trigger on build

Ulises Carreón asked in Rails

We have this model called "Cliente" (cliente.rb):

 class Cliente < ApplicationRecord
    has_many :clientes_hardwares
    has_many :clientes_notificaciones
    has_many :sucursales
    has_many :notificaciones,   through: :clientes_notificaciones
    has_many :hardwares,        through: :clientes_hardwares
end

The SQL table:

And the model "Notificaciones" (notificacione.rb):

   class Notificacione < ApplicationRecord
    has_many :clientes_notificaciones
    has_many :clientes, through: :clientes_notificaciones
end

The SQL Table:

And after that we created a join table.


class CreateJoinTableClientesNotificaciones < ActiveRecord::Migration[5.2]
  def change
    create_join_table :clientes, :notificaciones do |t|
      t.index [:cliente_id, :notificacione_id]
      t.index [:notificacione_id, :cliente_id]
    end
  end
end

The SQL table is called "clientes_notificaciones" and his structure very simple

The model is file(clientes_notificacione.rb):

class ClientesNotificacione < ApplicationRecord
    belongs_to :cliente
    belongs_to :notificacione
end

We wanna save the relation on the table but the console doesn't show the actual error.

def savenoti
         @cliente = Cliente.find(6)
        @cliente.clientes_notificaciones.build(
            :notificacione => Notificacione.find(1)
        )
        @cliente.save
 end

But the console shows:

wrong number of arguments (given 1, expected 0)

Am i missing something?

Regards in advance.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,344+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.