首页 > 解决方案 > 通过方法更新哈希以保存在 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

标签: ruby-on-railsruby

解决方案


也许您正在寻找类似的东西:

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论点的构造,则可能应该更改firstnamefirst_namelastnamelast_name以便您可以这样做:

def self.create_by_location(location,hash)
  user = User.new(hash)
end

推荐阅读