Ruby on rails 模型未将值保存到Rails中的历史模型
每次用户编辑或创建新配置时,我想将Ruby on rails 模型未将值保存到Rails中的历史模型,ruby-on-rails,Ruby On Rails,每次用户编辑或创建新配置时,我想将configs\u histories表中的所有值保存到历史记录表(configs\u histories)。因此,我在Config模型中使用after\u save回调调用save\u configs\u map\u to\u history函数。 但是,当我编辑配置时,rails无法将值保存在历史记录表中,在控制台中显示回滚事务。我做错了什么 请帮忙,提前谢谢 config.rb: class Config < ApplicationRecord
configs\u histories
表中的所有值保存到历史记录表(configs\u histories
)。因此,我在Config
模型中使用after\u save
回调调用save\u configs\u map\u to\u history
函数。
但是,当我编辑配置时,rails无法将值保存在历史记录表中,在控制台中显示回滚事务
。我做错了什么
请帮忙,提前谢谢
config.rb:
class Config < ApplicationRecord
after_save :save_configs_map_to_history
belongs_to :mirth
has_paper_trail
def save_configs_map_to_history
configs = Config.all
version_no = (ConfigsHistory.maximum('version') || 0) + 1
configs.each do |config|
ConfigsHistory.create!(key: config.key, value: config.value,
comment: config.comment, mirth_id: config.mirth_id,
version: version_no, config_created_at: config.created_at,
config_updated_at: config.updated_at, config_created_by: config.created_by,
config_updated_by: config.updated_by)
end
end
end
...
(0.0ms) SELECT MAX("configs_histories"."version") FROM "configs_histories"
Config Load (1.0ms) SELECT "configs".* FROM "configs"
Mirth Load (1.0ms) SELECT "mirths".* FROM "mirths" WHERE "mirths"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(27.0ms) rollback transaction
Rendering configs/_edit.html.erb within layouts/application
...
class ConfigsHistory < ApplicationRecord
belongs_to :mirth
belongs_to :user
end
配置历史记录。rb:
class Config < ApplicationRecord
after_save :save_configs_map_to_history
belongs_to :mirth
has_paper_trail
def save_configs_map_to_history
configs = Config.all
version_no = (ConfigsHistory.maximum('version') || 0) + 1
configs.each do |config|
ConfigsHistory.create!(key: config.key, value: config.value,
comment: config.comment, mirth_id: config.mirth_id,
version: version_no, config_created_at: config.created_at,
config_updated_at: config.updated_at, config_created_by: config.created_by,
config_updated_by: config.updated_by)
end
end
end
...
(0.0ms) SELECT MAX("configs_histories"."version") FROM "configs_histories"
Config Load (1.0ms) SELECT "configs".* FROM "configs"
Mirth Load (1.0ms) SELECT "mirths".* FROM "mirths" WHERE "mirths"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(27.0ms) rollback transaction
Rendering configs/_edit.html.erb within layouts/application
...
class ConfigsHistory < ApplicationRecord
belongs_to :mirth
belongs_to :user
end
类配置存储
class Config < ApplicationRecord
after_save :save_configs_map_to_history
belongs_to :mirth
has_paper_trail
def save_configs_map_to_history
configs = Config.all
version_no = (ConfigsHistory.maximum('version') || 0) + 1
configs.each do |config|
binding.pry
config_history = ConfigsHistory.create!(key: config.key, value: config.value,
comment: config.comment, mirth_id: config.mirth_id,
version: version_no, config_created_at: config.created_at,
config_updated_at: config.updated_at, config_created_by: config.created_by,
config_updated_by: config.updated_by)
end
end
end
class-Config
当您运行代码时,请检查rails控制台,它将停止,您将获得一个控制台来调试问题
在该窗口中,复制下一行,您将看到它失败
之后,只需执行config_history.errors并查看错误消息
这应该给你足够的信息来调试你的情况和解决你的错误
要知道如何使用pry,请检查此项
class Config < ApplicationRecord
after_save :save_configs_map_to_history
belongs_to :mirth
has_paper_trail
def save_configs_map_to_history
configs = Config.all
version_no = (ConfigsHistory.maximum('version') || 0) + 1
configs.each do |config|
binding.pry
config_history = ConfigsHistory.create!(key: config.key, value: config.value,
comment: config.comment, mirth_id: config.mirth_id,
version: version_no, config_created_at: config.created_at,
config_updated_at: config.updated_at, config_created_by: config.created_by,
config_updated_by: config.updated_by)
end
end
end
class-Config
当您运行代码时,请检查rails控制台,它将停止,您将获得一个控制台来调试问题
在该窗口中,复制下一行,您将看到它失败
之后,只需执行config_history.errors并查看错误消息
这应该给你足够的信息来调试你的情况和解决你的错误
要知道如何使用pry,请检查此问题我发现了问题!
配置存储.create
中缺少一个必填字段(user\u id
),无法保存该值,但未显示错误(需要找到一种方法输出此类错误)。谢谢你的时间和建议 我发现了问题!配置存储.create
中缺少一个必填字段(user\u id
),无法保存该值,但未显示错误(需要找到一种方法输出此类错误)。谢谢你的时间和建议 谢谢!我想试试:)我试过使用pry,但它对RubyMine不起作用。我使用Rubymine作为我的IDE。我想我需要用另一种方法找出错误。我尝试使用IDE进行调试,但仍然找不到问题:(我设法在Bash中使用了pry。当我运行config_history时,它的答案是“nil”。因此,执行config_history.errors只会为nil:NilClass提供“未定义的方法‘错误’”。我不明白为什么它是nill。你说的是什么意思:“在该窗口中,复制下一行,您将看到它失败。”?复制下一行?嘿,稍等片刻。注意一点。您想将所有配置保存到配置历史模型?或您创建或编辑的特定配置?谢谢!尝试一下:)我曾尝试使用pry,但它不能与RubyMine一起使用。我正在使用RubyMine作为我的IDE。我想我需要用另一种方式找到错误。我尝试使用IDE进行调试,但仍然找不到问题:(我在Bash中使用了pry。当我运行config_history时,它会以“nil”回答。因此,执行config_history.errors只会给出“nil:NilClass的未定义方法“errors”。我不明白为什么它是nill。当你说:“在那个窗口中复制你的下一行,你会看到它失败时,你是什么意思。“?复制下一行吗?嘿,稍等一下..注意一下..是否要将所有配置保存到config_历史模型?或保存您创建或编辑的特定配置?