kbob
kbob

Reputation: 45

update hash by method to save in db Rails

I am trying to update a hash that is being made when a csv is uploaded by a user, so that it saves the added key/value pair to the db.

How can I update the hash being made in the create_by_location method with the method check_enable_recordings

user model

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

Upvotes: 1

Views: 309

Answers (1)

jvillian
jvillian

Reputation: 20263

Perhaps you're looking for something like:

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

BTW, you don't seem to use the location argument anywhere. Maybe you're not showing us all the code.

Also, if you have control over the construction of the hash argument, you should probably change firstname to first_name and lastname to last_name so you can just do:

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

Upvotes: 1

Related Questions