1

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
7
  • There appears to be no question... Commented Aug 12, 2020 at 21:13
  • @jvillian I will update my question to be more specific, but my question is how am I able to update the user hash with the check_enable_recrodings method so that it saves into the db Commented Aug 12, 2020 at 21:20
  • You mean user model? Secondly, your check method is defined where? If it's in User then you shouldn't be creating a new user, you should be doing self.recorded = "1". Commented Aug 12, 2020 at 21:42
  • @tadman it is all in the User model, I didn't even realize that it was that simple of a fix. Thank You! if you put it as the answer below I will mark it correct Commented Aug 12, 2020 at 21:44
  • I'm supposing what you want to do is set a property on the user that is being saved and not create a completely new user with just one property set, yeah. Commented Aug 12, 2020 at 21:45

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

This needs to be self.recorded = 1 otherwise you're declaring a variable called recorded that gets thrown away.
@tadman Updated and thank you! My lack of understanding is showing through. I was thinking that recorded= would be a setter method generated by ActiveRecord based on the database table structure and, therefore, recorded = 1 would set the model attribute rather than generating a temporary variable. I guess that's all wrong???
While you can do things like if recorded and it will call the method, = never does. You must be explicit, doing self.recorded = 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.