Rails with Postgres and MySql

08 Feb 2015 | | rails

"If you can just get it setup, you are halfway there...or...its not the mountains ahead that wear you down, but rather the grain of sand in your shoe."

After a while when doing ROR, you start to thinking…“hey..maybe when I finally get a job…they probably are not using SQLite..it might be good experience to do your development with some other database. Two of the most popular are PostgreSql and MySql.

I installed and set up both on my MAC Yosemite.

MySQL

Here is the deal. I previously was playing with PHP and I had MAMP installed, which basiscally gives you a PHP/Apache/MySQL stack. I figured why not just use THAT MYSQL instance instead of installing a separate one.

Besides. I was not able to use MYSQL with my Rails applications ANYWAY until i sorted things out. The MAMP version the the MYSQL2 gem were not working together. Now..if you really want to get into the details…look at the code for the MYSQL2 gem. I am sure that you can just change some setting in there, and you would be fine. But I learned that after the fact.

Lets cut to the chase. If don’t want to uninstall your MAMP application(because…yeah…you are thinking that some day you will return to PHP!) then here is what you need to know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def use_MAMPs_MYSQL
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: root
  host: localhost
  socket: /Applications/MAMP/tmp/mysql/mysql.sock

development:
  <<: *default
  database: rails_mysql_development
  socket: /Applications/MAMP/tmp/mysql/mysql.sock

end

If you get some error in your rails setup like “Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ “ it’s because when you installed MAMP it has it’s own directories and location for the mysql.sock file.

I found that by just changing the config/database.yml to point to the correct mysql.sock file it works fine. Now..in my case, I had also installed a version of MYSQL with Homebrew. I did not bother to uninstall it. So I had the mysql.sock in another location. But..I think the mysql2 GEM looks for it in the default location anyway.

If you are having trouble with rails connecting, and you are using MAMP…try this.

PostGres

Coming from my notes ..too tired right now..gotta paint.

comments powered by Disqus(comments off)

Older · View Archive (32)

RVM tips

"Things I find most useful and hard to remember with RVM.."

RVM Tips

Anyone who sets up a ROR rails environment sooner or later will bet directed by some link or comment to RVM. I for one initially found the website documentation hard to read and find things, but I guess that’s because I did not know RVM.

Usually when you are learning rails, the last thing you want to do is start up yet another learning task. But this is one that probably you need to do first, and save the others for when you get going.

I will list the things I find most convenient and helpful about RVM. Most for my own reminding because there are tons of sites out there that go way more into it. There are other things out there(rbenv being one), and I actually wanted to try it. But it is not compatible with RVM on the same box, so go figure. You can’t go wrong with any, just choose one.

RVM implode

This may happen to you, and it did happen to me. I somehow got my environment messed up, or I guess you could say somehow my gems and dependencies got out of sync. No matter what I tried, I kept getting some weird error message everytime I tried to build and run my application.

I figured i needed to start from scratch with a fresh install. But how?

Used rvm implode from the terminal to remove all traces of RVM. If for some reason it does not get everything, it will tell you just to manually delete your .rvm directory.

After that, just reinstall RVM. By the way, this action will also remove all of your existing GEMS etc that hopefully you installed with RVM in the first place. Install RVM(which will install ruby), and then install rails. With each you do have the option to say what version of ruby and rails you want, and in fact, in my case I went back to one that I know was working with what I was previously doing. I think somehow the latest version of Ruby was not playing well with my Jekyll.

1
2
3
4
def use_RVM
puts "rvm implode"
puts "To remove all traces of RVM installs and start over"
end

What RVM is good for

In a nutshell, RVM will let you switch between different versions of Ruby. This is good when you want to check out something under an older version(maybe you are maintaining an application that is not upgraded yet), and also when a new release comes out. You want to be cautious just in case it breaks something in all the things you are currently working on.

Secondly, RVM lets you manage your GEMS as collections. As you know a rails application uses many gems, all with versions. And with many of them having dependencies on other gems, just updating or installing one can quick lead to some kind of mess where you don’t know where you are, especially with something does not work.

So what you can do is, for each new Rails application, create a “gemset” that will be a compartmentalized environment built just for that application. In that way when you install or update the gems, it will have no impact on the other apps you may be working on.

Now to me, that always seemed a bit redundant, in that often times you are installing the same gems over and over. But in the long run, using this little bit of extra storage will pay off in a more clear and trouble-free workflow.

Here are some quick RVM commands to know before we get into setting up gemsets:

1
2
3
rvm list(to see a list or Ruby versions on your machine)
rvm gemset list(to see a list of gemsets)
rvm gemset use gemsetname(switch between gemsets)

Creating Gemsets

Let’s say you are checking out a Rails application you download from Railscasts, and its asking for some GEM version. You jump into terminal and grab it. Did you know that without using Gemsets, you just updated or changed that Gem for your other rails apps you may be working on? Thats because without Gemsets, your “rails environment” which is composed of gems may change each time you update or download a gem.

If you use the RVM gemset feature for each project, then each project will have its own specific set/versions of rails. So the way I do it, for newer stuff where I always want the latest, I use the latest gems.

But in some cases when I am testing out something that maybe be several releases old, I will set up an environment just for that app.

One way to create a gemset is as follows: Let’s say you have ruby-2.2.0 and want a gemset for your new app call “myapp”

1
2
3
rvm use ruby-2.2.0@myapp --create
gem install rails 
rails -v

This will create a gemset with the name “ruby-2.2@myapp” using ruby 2.2 and the version of rails installed. Each time before you go into your app directory, you will issue the rvm USE command, unless you create a “sticky gemset” which I recommend.

If you want a specific version of rails, change the “gem install rails” to “gem install rails –version=##.##” where ## is the version number.

Sticky Gemsets

As you saw once you create a gemset, you need to set it or “use” it each time before you go into your app directory. A better way is to create what is called a ‘sticky gemset.”

RVM gives us a convenient technique to make sure we are always using the correct gemset when we enter the project directory. It will create hidden files to designate the correct Ruby version and project-specific gemset.

Enter this command to create the hidden files:

1
rvm use ruby-2.2@myapp --ruby-version

This will create

1
2
.ruby-gemset
.ruby-version

The –ruby-version argument creates two files, .ruby-version and .ruby-gemset, that set RVM every time we cd to the project directory. Without these two hidden files, you’d need to remember to enter rvm use ruby-2.2@myapp every time you start work on your project after closing the console.

(see RVM website on using gemsets)

Newer

Why I like Rubymine

“I like tools and going to Home Depot to look at them. Even buy unfortunately.”