【发布时间】:2019-02-09 23:54:29
【问题描述】:
我们有这个模型叫做“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
SQL 表:
和模型“Alerta”(alerta.rb):
class Alerta < ApplicationRecord
has_many :alertas_clientes
has_many :clientes, through: :alertas_clientes
end
SQL 表:
然后我们创建了一个连接表。
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
SQL 表名为“alertas_clientes”,其结构非常简单
模型是文件(alertas_cliente.rb):
class AlertasCliente < ApplicationRecord
belongs_to :cliente
belongs_to :alerta
end
我们想将关系保存在表格上,但控制台没有显示实际错误。
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
但控制台显示:
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)
我错过了什么吗?
提前问候。
【问题讨论】:
-
你能展示alertas_controller.rb的相关部分吗?特别是第 39 行和第 41 行的函数?看起来第 41 行的任何内容都是导致您的错误的原因。如果您在上面的代码中已经有了这些行,您能指出它们是哪些行吗?你也可以在你的控制器中添加
logger.debug "value of foo is:#{@foo}"在给你错误的行之前,并检查你的控制台日志,看看它是否试图保存你认为它正在保存的东西。它表示您正在为不期望的方法提供参数。 -
感谢@Beartech 的回复,问题是字段名称是一个保留字
标签: ruby-on-rails ruby activerecord