Using .find_by_sql for database/admin queries

I want to create an internal admin view to display the output of “show
variables” from mysql. What’s the best approach do this?

For example, I’m doing something like this:

@variables = ActiveRecord::Base.find_by_sql “show variables;”

Then, I get stuff like this back (227 elements in @variables) in
script/console:

@variables[0]
=> #<ActiveRecord::Base:0x3296eec
@attributes={“Variable_name”=>“auto_increment_increment”, “Value”=>“1”}>

However, I’m not sure how to get Variable_name and Value; do I access
this as a Hash element? An attribute?

Should I create a custom Model class such as this?
class MySql < ActiveRecord::Base

Thanks in advance.

@variables[0]
=> #<ActiveRecord::Base:0x3296eec
@attributes={“Variable_name”=>“auto_increment_increment”, “Value”=>“1”}>

However, I’m not sure how to get Variable_name and Value; do I access
this as a Hash element? An attribute?

What happens if you do:

@variables[0][:Variable_name]
@variables[0][:Value]

?

Philip H. wrote:

@variables[0]
=> #<ActiveRecord::Base:0x3296eec
@attributes={“Variable_name”=>“auto_increment_increment”, “Value”=>“1”}>

However, I’m not sure how to get Variable_name and Value; do I access
this as a Hash element? An attribute?

What happens if you do:

@variables[0][:Variable_name]
@variables[0][:Value]

?

Hi, Philip. Thanks. Unfortunately, I get the same error as before
(since I had already tried a variation: @variables[0][“Variable_name”])

@variables[0][:Variable_name]
NoMethodError: undefined method abstract_class?' for Object:Class from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/base.rb:1367:inclass_of_active_record_descendant’
from
./script/…/config/…/config/…/vendor/rails/activerecord/lib/activ

Ben K. wrote:

@variables[0]

Try something like:

ActiveRecord::Base.connection.select_all “show variables”


Jack C.
[email protected]

Jack C. wrote:

Ben K. wrote:

@variables[0]

Try something like:

ActiveRecord::Base.connection.select_all “show variables”


Jack C.
[email protected]

Thanks, Jack – that did the trick! Here is some sample output:

@variables[0]
=> {“Variable_name”=>“auto_increment_increment”, “Value”=>“1”}

@variables[0][“Variable_name”]
=> “auto_increment_increment”

@variables[0][“Value”]
=> “1”