Rorra's blog Senior Developer

July 30, 2010

Rails 3, mysql and UTF-8

Filed under: Ruby — admin @ 3:01 PM


So, have you started to work with rails 3? Did you realize that your UTF-8 databases doesn’t work nice with mysql? This is because the mysql gem works with ASCII-8BIT encoding, but ruby 1.9 and rails 3 works with UTF-8 encoding, so when you create a model into the database, everything works fine, but when you work with those models, it doesn’t works so nice and you don’t get what you stored.
What’s the solution? Well, there are actually three solutions, the most recommended, use mysql2, in order to do this, edit your Gemfile and include:

gem "mysql2"

and then, edit your databases.yml file, and change the adapter to mysql2

development:
  adapter: mysql2
  database: fun_development
  user: root
  password:
  encoding: utf8

Another solution, is just instead of using the “mysql” gem, use the “ruby-mysql” gem, but it’s pretty slow because it’s a 100% ruby gem.

gem "ruby-mysql"

The last solution is a monkey patch, but its also slow to use it, so I really recommend using the mysql 2 gem

require 'mysql'

class Mysql::Result
  def encode(value, encoding = "utf-8")
    String === value ? value.force_encoding(encoding) : value
  end

  def each_utf8(&block)
    each_orig do |row|
      yield row.map {|col| encode(col) }
    end
  end
  alias each_orig each
  alias each each_utf8

  def each_hash_utf8(&block)
    each_hash_orig do |row|
      row.each {|k, v| row[k] = encode(v) }
      yield(row)
    end
  end
  alias each_hash_orig each_hash
  alias each_hash each_hash_utf8
end

July 29, 2010

Contributing to ruby on rails source code is so easy

Filed under: Ruby — admin @ 1:59 PM




I finally made my first contribution to rails, although it’s not a big fix nor anything like that, is a way to get started.


If you have read my last posts, you will see how to install and works with rails edge, but now I’ll explain how to setup a good rails environment to live in the edge and to help ruby on rails community by checking the issues posted on lighthouseapp.com to see if they are a real bug or not, to create patches for bugs, or why not, to create a new functionality for the rails framework.


Supposing that you already installed the last version of rails, you will need to get the rails code checked out from github, and make your demo project to use that version, so you can modify that version to create fixes, to test fixes, etc.

git checkout http://github.com/rails/rails.git



Now that you just create an application for doing test/development

rails new my_app



Edit the Gemfile of my_app (my_app/Gemfile) to use the rails code you are going to work with, by giving the rails gem a parameter :path where your code of ruby on rails is installed

gem 'rails', :path => File.expand_path('../../rails',  __FILE__)



And that’s all, now you can follow this guide to know how to work with git in order to post valid patches.

July 27, 2010

Rails 3.0 RC Released!!!!

Filed under: Ruby — admin @ 12:00 PM

So finally, rails 3.0 RC was released, cool news because rails 3.0 brings a lot of updates and it will be very fun to develop for rails 3.0. I wonder how much work would be out there migrating rails 2.x to rails 3.x versión.
Rails 3.0 RC is out just after 21 hours bunder 1.0.0.RC.1 was released, was it really a coincidence? Yeah sure…
So how to start playing with rails 3.0.RC? Pretty easy, first, install rvm, after that, lets install ruby 1.9.2 RC 2

rvm install 1.9.2

Now let’s use ruby 1.9.2

rvm 1.9.2

Then we will install bundler, because we are using rvm, we won’t use sudo at anytime

gem install bundler -v 1.0.0.rc.1

And now we can install rails rc 1

gem install rails --pre

We can start playing with rails 3.0

rails app my_app_name
cd my_app_name
bundle install

But finally, instead of locking your development on rails 3.0.RC 1, work with the latest version of rails that is being developed (because we will find bugs in the RC), edit the Gemfile inside your application, and do these modifications

#gem 'rails', '3.0.0.rc'

# Bundle edge Rails instead:
gem 'rails', :git => 'git://github.com/rails/rails.git'

Now update your app to work with the latest version of rails

bundle install

And that’s all :)
You can report any bugs you find on https://rails.lighthouseapp.com/projects/8994-ruby-on-rails, and also check the bugs there, reproduce them and confirm that is really a bug or not, and don’t forget, you are also testing bundler 1.0.0.RC.1, so if you find any issue with bundler, you can report your issues on http://github.com/carlhuda/bundler/issues.
Remember, this is your opportunity to give something back to the open source community.

July 24, 2010

Installing RubyDebug with ruby 1.9.2 and rails 3 to use RubyMine

Filed under: Ruby — admin @ 5:26 PM


So you are trying to install ruby-debug to debug rails applications (or perhaps ruby-debug-ide for RubyMine) and you are having problems with it, because you have ruby 1.8.7 for your system, and you are using rvm in order to user ruby 1.9.2 to test rails edge.

The first thing to notice, is that all of the sources that you have installed with rvm are located in $rvm_path/src, so in case you are using ruby 1.9.2 rc 2 as I’m doing right now, the installation is pretty easy

gem install ruby-debug-ide19 -- --with-ruby-include=$rvm_path/src/ruby-1.9.2-rc2/

But for sure, if you are using ruby 1.9.1 or any other version, you can easily do a ls $rvm_path/src to find the right location to compile ruby-debug-ide19 into your system.

Now, you can add the gem to the Gemfile if you are planning to debug from the console

gem 'ruby-debug19', :require => 'ruby-debug', :group => :development

And then run

rails s --debug

Or if you are using rubymine, the debug will start although it’s not working yet, hopefully it will be working before rubymine 2.3.5 is released, because nobody wants to use rails3 without ruby 1.9.2, or at least I don’t want to do that :P

Powered by WordPress