ruby-on-rails - Ruby 模型构造函数给出错误:传递的参数数量错误
问题描述
我在 macOS 上使用 Rails 6.0。我的班级定义如下:
class Asset < ApplicationRecord
def initialize (symbol, name, type, listed_on, faceValue)
@name=name
@symbol=symbol
@type=type
@faceValue=faceValue
@listed_on=listed_on
p "Insde Constructor"
end
end
我正在尝试从 Rails 控制台实例化它
解决方案
不要乱用 ActiveRecord 的初始化方法。如您所见,坏事发生了。相反,用于attr_accessor
添加非列字段。
class Asset < ApplicationRecord
attr_accessor :symbol, :name, :type, :listed_on, :faceValue
end
这允许访问列和您的额外属性。例如,如果资产表具有列成本...
asset = Asset.new(
cost: 12.34, # set the cost column
name: "Junk",
type: :junk,
listed_on: Time.current,
faceValue: 0,
symbol: :jnk
)
如果您真的需要搞乱初始化,请使用after_initialize 回调。
如果 symbol、name、type、listed_on 和 faceValue 都是 assets 表的列,则什么也不做。ActiveRecord 会处理它。
class Asset < ApplicationRecord
end
asset = Asset.new(
name: "Junk",
type: :junk,
listed_on: Time.current,
faceValue: 0,
symbol: :jnk
)
如果没有资产表,则根本不要从 ActiveRecord 继承。
class Asset
def initialize (symbol, name, type, listed_on, faceValue)
@name=name
@symbol=symbol
@type=type
@faceValue=faceValue
@listed_on=listed_on
p "Insde Constructor"
end
end
asset = Asset.new("Junk", :junk, Time.current, 0, :jnk)
但我会说使用命名参数,这样人们就不必记住参数 4 的含义。
class Asset
def initialize (symbol:, name:, type:, listed_on:, faceValue:)
@name=name
@symbol=symbol
@type=type
@faceValue=faceValue
@listed_on=listed_on
p "Insde Constructor"
end
end
asset = Asset.new(
name: "Junk",
type: :junk,
listed_on: Time.current,
faceValue: 0,
symbol: "jnk"
)
这可以通过包含ActiveModel::Model更轻松地完成。你会得到很多方便的东西,比如验证。
class Asset
include ActiveModel::Model
attr_accessor :symbol, :name, :type, :listed_on, :faceValue
validates :name, :type, :symbol, presence: true
validates :faceValue, numericality: { greater_than_or_equal_to: 0 }
end
asset = Asset.new(
name: "Junk",
type: :junk,
listed_on: Time.current,
faceValue: 0,
symbol: :jnk
)
推荐阅读
- java - 我的 servlet 不工作我收到错误 HTTP404-not found 但我的 jsp +html 文件工作正常
- javascript - 使用初始状态反应 useState 钩子事件处理程序
- python - NameError: X while execution WebLogic Scripting Tool (WLST) script
- pandas - Shift 发生:使用 pandas shift 来组合行
- jms - 使用 Spring、ActiveMQ 和 Qpid 进行集成测试
- kotlin - 将 kotlin 中的扩展功能添加到所有类
- html - 响应式文本定位?
- c# - 多次打开 WPF 应用程序
- acumatica - Acumatica REST API - StockItem - 如何在请求中使用多个产品 ID
- nginx - 例程:SSL23_GET_SERVER_HELLO:尝试使用代理连接到 Neo4j 服务器时的未知协议