How do you manually execute SQL commands in Ruby On Rails using NuoDB

How do you manually execute SQL commands in Ruby On Rails using NuoDB

The working command Im using to execute custom SQL statements is:

results = ActiveRecord::Base.connection.execute(foo)

with foo being the sql statement( i.e. SELECT * FROM table).

This command will return a set of values as a hash and put them into the results variable.

So on my rails application_controller.rb I added this:

def execute_statement(sql)
  results = ActiveRecord::Base.connection.execute(sql)

  if results.present?
    return results
  else
    return nil
  end
end

Using execute_statement will return the records found and if there is none, it will return nil.

This way I can just call it anywhere on the rails application like for example:

records = execute_statement(select * from table)

execute_statement can also call NuoDB procedures, functions, and also Database Views.

For me, I couldnt get this to return a hash.

results = ActiveRecord::Base.connection.execute(sql)

But using the exec_query method worked.

results = ActiveRecord::Base.connection.exec_query(sql)

How do you manually execute SQL commands in Ruby On Rails using NuoDB

Reposting the answer from our forum to help others with a similar issue:

@connection = ActiveRecord::Base.connection
result = @connection.exec_query(select tablename from system.tables)
result.each do |row|
puts row
end

Leave a Reply

Your email address will not be published.