0

My model

after_create :generate_token
def generate_token
  self.token = Digest::SHA1.hexdigest(Time.now.to_s + self.id.to_s)
  self.save
end 

When I trying

Model.find_by_token(params[:token])

returned nil.

But if I make

@t = Model.last
@t.token = "ec088e7de0c1e25997f1a2e417c9b9bd150ea7a0"
@t.save

And I can find this record by

Model.find_by_token(params[:token])

where params[:token] is "ec088e7de0c1e25997f1a2e417c9b9bd150ea7a0"

Also it works when simple

def generate_token
  self.token = rand(100).to_s + self.id.to_s
  self.save
end
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
Vdimir
  • 123
  • 1
  • 5
  • What is the saved token? what is is `params[:token]`? does it exist in the database? – Uri Agassi May 12 '14 at 12:18
  • If I look `Model.all ` I see right tokens. also params[:token] is right – Vdimir May 12 '14 at 12:22
  • So you are saying the `Model.find_by_token` is not working properly? – Uri Agassi May 12 '14 at 12:23
  • `Model.find_by_token` do not work it return nil, if token is fielded by `Digest::SHA1.hexdigest` – Vdimir May 12 '14 at 12:24
  • can u show me the console query? when retrieving by find_by_token – santosh May 12 '14 at 12:27
  • [console](http://imgur.com/VQztWWB) – Vdimir May 12 '14 at 12:39
  • You should find out how to copy text out of your console, looking at a picture is not helpful when you want to compare two long random-looking text strings. – Max Williams May 12 '14 at 12:42
  • [pastebin](http://pastebin.com/6QGQCkm4) – Vdimir May 12 '14 at 12:52
  • In your console what happens if you do `the_token = Task.first.token` then `Task.find_by_token(the_token)`? –  May 12 '14 at 13:07
  • [pastebin](http://pastebin.com/gK77nLRa) – Vdimir May 12 '14 at 13:11
  • Wierd; that's proved there is no copy/paste problem. Can you do the query directly to the database i.e. not through the rails console? Which database are you using? I have a table in MySQL with SHA256 used to generate one field and I can successfully use a dynamic find_by method. –  May 12 '14 at 13:30
  • I use default db sqlite [pastebin](http://pastebin.com/mU5JAR7h) I think it is problem with db – Vdimir May 12 '14 at 13:50
  • Well, I find some information about this problem [here](https://groups.google.com/forum/#!topic/sqlite3-ruby/40GmnmULOCQ) – Vdimir May 13 '14 at 17:28

0 Answers0