How to run a simple MYSQL query using rails

I want to run a simple MYSQL query using rails

Select movie-title, movie-director from moving order by rating desc limit 5; 

I do not need all overhead models. I just want to run a query.

What is the best way to do this?
I can’t even connect

Here is my code from my controller

 ActiveRecord::Base.establish_connection ({ :adapter => "mysql2", :host => "some-rds.amazon.com", :username => "root", :password => "root", :database => "blah"}) 

This will cause this ActiveRecord :: ConnectionNotEstablished error to occur.

thanks

+4
source share
3 answers
 movie_title = 'Planet of the Apes' sql = "SELECT * FROM movies WHERE title = #{ActiveRecord::Base.sanitize(movie_title)}" ActiveRecord::Base.connection.execute(sql) 
+9
source

Just use ModelName.find_by_sql ("your SQL query") in the console.

+2
source

Hey for the cool fist, make sure you have eight gems and install support for Mysql:

Customizing Rails Applications

In this section, we will modify the Rails application servers to start working with the database server that we just created.

Install Database Server Libraries

The first thing to do is install the required database libraries. In our case, this is the MySQL development package.

Run the following to install the mysql-devel MySQL development package:

 yum install -y mysql-devel 

Setting Up .yml Database For Rails

The database settings for Rails applications are stored inside the database.yml file in the / config directory.

Run the following command to edit the database.yml file with a text editor:

 # Make sure to enter your application deployment directory # Example: # cd /var/www/my_app nano config/database.yml 

As soon as you open this file, you will see the database settings separated by environment names. Since the application must be started using the working environment, edit the configuration for this.

Replace the YML production block as follows, changing the necessary bits according to your own configuration setting, for example. IP address etc.

 # Example: # production: # adapter: mysql # encoding: utf8 # database: [database name] # username: [user name] # password: [password] # host: [server IP address] # port: [port number] # protocol: [protocol] # pool: [connection pool] production: adapter: mysql encoding: utf8 database: rails_myapp username: rails_myapp_user password: pwd host: 128.199.233.36 port: 3306 pool: 10 

Note. As shown in the example above, you may need to specify a Protocol.

Note. The pool argument contains the number of maximum simultaneous database connection slots available (i.e. pool). You must evaluate your needs and set the number accordingly.

Save and exit by pressing CTRL + X and confirming with Y.

Getting mysql image

Start editing the gemfile with nano using the following:

 nano Gemfile 

Add the following line to the file:

 gem 'mysql' 

Save and exit by pressing CTRL + X and confirming with Y.

Install new pearls with the package:

 bundle install 

And this! From now on, Rails application servers will use your new database server for all operations.

You can find more information at: https://www.digitalocean.com/community/tutorials/scaling-ruby-on-rails-setting-up-a-dedicated-mysql-server-part-2

0
source

Source: https://habr.com/ru/post/1501729/


All Articles