I want run a simple MYSQL query using rails
Select movie-title, movie-director from moving order by rating desc limit 5;
I don't want all the overhead creating models. I just want to run the query.
What is the best way to do this?
I cannot even connect
Here is my the code from my controller
ActiveRecord::Base.establish_connection ({
:adapter => "mysql2",
:host => "some-rds.amazon.com",
:username => "root",
:password => "root",
:database => "blah"})
This will generate this error ActiveRecord::ConnectionNotEstablished
thanks
Ruby on Rails uses SQLite as its database by default, but it also supports the use of MySQL.
To access a specific database, type the following command at the mysql> prompt, replacing dbname with the name of the database that you want to access: Copy use dbname; Make sure you do not forget the semicolon at the end of the statement. After you access a database, you can run SQL queries, list tables, and so on.
movie_title = 'Planet of the Apes'
sql = "SELECT * FROM movies WHERE title = #{ActiveRecord::Base.sanitize(movie_title)}"
ActiveRecord::Base.connection.execute(sql)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With