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.

Reply
Join the discussion

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.