ruby-on-rails - 通过方法更新哈希以保存在 db Rails 中
问题描述
我正在尝试更新用户上传 csv 时生成的哈希,以便将添加的键/值对保存到数据库中。
如何create_by_location
使用该方法更新该方法中的哈希值check_enable_recordings
用户模型
before_save :check_enable_recordings
def check_enable_recordings
x = tenant.enable_recording_extensions
Rails.logger.debug("check if true #{x}" )
if x
user = User.new(
recorded: "1"
)
end
end
def self.create_by_location(location,hash)
user = User.new(
first_name: hash[:firstname],
last_name: hash[:lastname],
email: hash[:email],
)
end
解决方案
也许您正在寻找类似的东西:
before_save :check_enable_recordings
def check_enable_recordings
self.recorded = 1 if tenant.enable_recording_extensions
end
def self.create_by_location(location,hash)
user = User.new(
first_name: hash[:firstname],
last_name: hash[:lastname],
email: hash[:email],
)
end
顺便说一句,你似乎没有在location
任何地方使用这个论点。也许您没有向我们展示所有代码。
此外,如果您可以控制hash
论点的构造,则可能应该更改firstname
为first_name
和lastname
,last_name
以便您可以这样做:
def self.create_by_location(location,hash)
user = User.new(hash)
end
推荐阅读
- amazon-web-services - 是否可以重命名或更改 AWS codedeploy appspec 文件的目录?
- excel - 使用 Excel-VBA 和 ADODB 连接在 OneDrive 上打开 Access 数据库
- azure - 我们可以在同一个公共 IP 的不同子域中使用相同的端口来托管两个不同的应用程序吗
- python - 根据列条件排列不带时间戳的时间序列数据:
- python - 只获取异常,而不是整个堆栈跟踪
- angular - 使用firebase构建角度项目时出错
- php - Google Sheets API - 更新了 0 个单元格
- java - 为什么我的 JUnit 测试从 Android Studio 运行但从 CLI 失败?
- python - 运行从中断处开始的脚本 - Python
- java - Java 接口设计:我应该把所有子类都使用的大量重复代码放在哪里?