EamonnMcElroy
EamonnMcElroy

Reputation: 607

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.

Upvotes: 5

Views: 6581

Answers (1)

Elena  Unanyan
Elena Unanyan

Reputation: 1199

Can you run in rails console? Have you result?

result = ActiveRecord::Base.connection.execute(sql)
result.to_a

Upvotes: 8

Related Questions