Rails 3 execute custom sql query without a model
ruby-on-rails, sql
Solution
Maybe try this:
ActiveRecord::Base.establish_connection(...)
ActiveRecord::Base.connection.execute(...)
Problem
I need to write a standalone ruby script that is supposed to deal with database. I used code given below in rails 3 ``` @connection = ActiveRecord::Base.establish_connection( :adapter => "mysql2", :host => "localhost", :database => "siteconfig_development", :username => "root", :password => "root123" ) results = @connection.execute("select * from users") results.each do |row| puts row[0] end ``` but getting error:- ``` `<main>': undefined method `execute' for #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00000002867548> (NoMethodError) ``` what i am missing here? SOLUTION After getting solution from denis-bu i used it following way and that worked too. ``` @connection = ActiveRecord::Base.establish_connection( :adapter => "mysql2", :host => "localhost", :database => "siteconfig_development", :username => "root", :password => "root123" ) sql = "SELECT * from users" @result = @connection.connection.execute(sql); @result.each(:as => :hash) do |row| puts row["email"] end ```