如何解决通过方法更新哈希以保存在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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。