Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Need help with model

md5 hash gravatar model activerecord error

  • Please log in to reply
1 reply to this topic

#1 chrisdesrochers

chrisdesrochers

    Passenger

  • Members
  • 1 posts
  • LocationDallas, Texas

Posted 13 May 2014 - 02:21 AM

I'm having trouble with my model. The goal is to take column email and do the following (1) trim the string (2) make it lowercase and (3) create a MD5 hash. I keep getting a syntax error. Any suggestions? 

 

require 'digest'


class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable


validates :first_name, presence: true
validates :last_name, presence: true


# Normalize email and create MD5 hash for Gravatar
# Reference: https://en.gravatar.com/site/implement/hash/

before_save :md5_hash

private
   
   def md5_hash (hash)
    @hash = hash
    @hash = tokens[self.email]
    @hash.strip! # Trim the string
     @hash = @hash.downcase # Make email lowercase
    @hash = Digest::MD5.digest(@hash) # Create a MD5 hash
     self.email_md5 = @hash
    end
    
end
 


#2 Ohm

Ohm

    Driver

  • Moderators
  • 468 posts
  • LocationCopenhagen

Posted 13 May 2014 - 05:09 AM

What errors are you getting?

 

Why not just do it like this

...

before_save :md5_hash

...

private

def md5_hash
  self.email_md5 = Digest::MD5.hexdigest(self.email.strip.downcase)
end

...

Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk






Also tagged with one or more of these keywords: md5, hash, gravatar, model, activerecord, error

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users