【问题标题】:Model new with params, initialize all attributes with nil用 params 新建模型,用 nil 初始化所有属性
【发布时间】:2017-05-03 17:02:18
【问题描述】:

我用一些属性值初始化一个 ItemMedida 对象

item_medida = SpfPedido::ItemMedida.new(cantidad: 3, ancho: 800, alto: 600)

但是当我调用它时,所有属性都是 nil

item_medida #=> <SpfPedido::ItemMedida id: nil, cantidad: nil, ancho: nil, alto: nil)

cantidad, alto, ancho 在 DB 中定义良好。

为什么我会出现这种行为?我希望我的对象初始化为正确的属性。 有什么建议吗?

型号

# encoding: utf-8
module SpfPedido
  class ItemMedida < ApplicationRecord
    belongs_to :item, class_name: "SpfPedido::Item", inverse_of: :item_medidas
    belongs_to :medida
    has_many :pulidos, class_name: "SpfPedido::Pulido"
    has_many :agujeros, class_name: "SpfPedido::Agujero"
    has_many :entrantes, class_name: "SpfPedido::Entrante"
    has_many :solapes, class_name: "SpfPedido::Solape"
    has_one :forma
    has_and_belongs_to_many :adjuntos
    has_many :adjunto_medidas
    has_many :adjuntos, through: :adjunto_medidas
    has_many :curvaturas
    has_many :bites
    has_many :tango_bodies, as: :linea_item, class_name: 'SpfTango::TangoBody'
    # Polymorphic
    has_one :pre_facturacion, as: :linea_item
    has_one :facturacion, as: :linea_item
    has_one :expedicion, as: :linea_item
    has_many :trazabilidads, through: :item
    #############
    has_and_belongs_to_many :orden_corte_mesas # creo que no va mas
    has_many :medidas_orden_corte_mesas
    has_many :comprobante_medidas
    has_many :comprobante_temps, through: :comprobante_medidas
    has_many :rotura_vidrios
    has_many :elementos, through: :item
    has_many :proceso_de_elementos, through: :item
    has_many :componentes, through: :item
    has_many :proceso_de_componentes, through: :item
    delegate :pedido, to: :item
    before_update :cambio_de_medidas?, :if => proc { ancho_changed? || alto_changed? } 
    validates :cantidad, numericality: { only_integer: true, greater_than: 0, less_than: 1000, message: "Debe introducirse un valor entero entre 0 y 999" } 
    validates :ancho, numericality: { only_integer: true, greater_than: 0, less_than: 10000, message: "Debe introducirse un valor entero entre 0 y 9999" }
    validates :alto, numericality: { only_integer: true, greater_than: 0, less_than: 10000, message: "Debe introducirse un valor entero entre 0 y 9999"  }
    accepts_nested_attributes_for :medida
    accepts_nested_attributes_for :solapes
    accepts_nested_attributes_for :pulidos
    accepts_nested_attributes_for :bites
    accepts_nested_attributes_for :curvaturas
    attr_accessor :config_pulido_id
    attr_accessor :objeto_pulible_type
    attr_accessor :objeto_pulible_id
    attr_accessor :accion
    attr_accessor :cambio_de_medidas
    attr_accessor :comentario
    attr_accessor :total_de_complementos # es la suma de complementos del item
    attr_accessor :precio_unitario_con_complementos # total_linea + total_complementos / superficie
  end
end

我希望 item_medida 成为 &lt;SpfPedido::ItemMedida id: nil, cantidad: 3, ancho: 600, alto: 800&gt;

编辑:

另一个测试。

item_medida = SpfPedido::ItemMedida.new(cantidad: 3, ancho: 800, alto: 600)
item_medida #=> <SpfPedido::ItemMedida id: nil, cantidad: nil, ancho: nil, alto: nil>
item_medida.valid? #=> false
item_medida = SpfPedido::ItemMedida.new(cantidad: 3, ancho: 800, alto: 600)
item_medida #=> <SpfPedido::ItemMedida id: nil, cantidad: 3, ancho: 800, alto: 600>

调用.valid?方法后,item_medida对象初始化正确。

【问题讨论】:

  • 不清楚Object是什么,这是AR对象吗?它是在哪里定义的?
  • 我不确定你想要达到什么目的。您想将该值保存在Object 中吗?或ActiveRecord
  • ItemMedida 继承自 ActiveRecord。
  • 我已经编辑了标题和问题内容
  • 我无法复制。

标签: ruby-on-rails ruby activerecord


【解决方案1】:

我发现了异常。这是我的错误。我的模型包含一个模块,其中有一个错误的 method_missing 重定义,没有使用 super,像这样:

def method_missing(m, *args, &block)
  if condition
    #do something
  end
end

所以我已经更正了

def method_missing(m, *args, &block)
  if condition
    #do something
  else
    super(m, *args, &block)
  end
end

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 2013-12-16
    • 2018-11-28
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    相关资源
    最近更新 更多