Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord::Base.connection.execute(sql).each not returning data

As part of an API application I am working on there is authentication required to make calls. I am attempting the most basic call with a curl command including the header basic authentication. In the authenticate block, which is called when a user attempts to access any secure calls, there is the following code:

unless credential = cache.fetch("credential::#{auth.credentials[0]}:#{Digest::MD5.hexdigest(auth.credentials[1])}")
  sql = "SELECT * FROM credentials WHERE username = '#{auth.credentials[0]}' AND passwd_hashed = '#{Digest::MD5.hexdigest(auth.credentials[1])}'"
  ActiveRecord::Base.connection.execute(sql).each do |row|
    credential = row
  end
end

For some reason this is not returning any results. I have taken the sql outputted from this block and ran it in postgres where it returns the desired row. I have tested my environment to make sure I am connecting to the correct database which I am. Beyond that I am not sure what the problem is or how to diagnose it. Any help would be appreciated.

Any suggestions as to how I could return an error from this block would also help greatly. Tried adding exception handling but cant seem to get the syntax correct.

like image 817
EamonnMcElroy Avatar asked Mar 11 '23 17:03

EamonnMcElroy


1 Answers

Can you run in rails console? Have you result?

result = ActiveRecord::Base.connection.execute(sql)
result.to_a
like image 139
Elena Unanyan Avatar answered Apr 28 '23 18:04

Elena Unanyan