UTF8 encoding with Ruby Sequel (and MySQL)

Just a quick note to self. If you need to store Unicode and are using Sequel, make sure you:

  • Create the database with a default charset of UTF8: CREATE DATABASE my_db DEFAULT CHARSET utf8;. I think then any tables you create uses the default charset of utf8 unless otherwise specified.
  • Pass an :encoding option to Sequel’s MySQL adapter:
    DB = Sequel.mysql 'my_db', :user => 'root', :password => '', :host => 'localhost', :encoding => 'utf8'

The Sequel documentation and wiki were quite unhelpful but reading code (see the connect method definition) always helps!

Living on the edge (of Rails) #11

This week’s report covers changes from 3 Mar 2008 to 9 Mar 2008 (the day the corresponding Rails Envy podcast was recorded).

Improve performance on :include/:conditions/:limit queries by selectively joining in the pre-query

This is another ActiveRecord performance boost related to the pre-loading any eager-loaded :includes mentioned previously. Basically what this patch does is to only join referenced tables when needed. It does this by checking the :conditions and :limit options to determine whether a table should be joined or can be left out and pre-loaded instead. You can find out more in tickets #9560 and #9497.

Credit goes to Gabe da Silveira (dasil003 on Trac) for this awesome performance patch.

Related changeset: http://dev.rubyonrails.org/changeset/8977

Better error message for type errors when parsing request parameters

Now when you pass an incorrectly formed request (e.g. GET, POST) parameter to your controller, it will raise an exception that includes a friendlier error message that indicates exactly what you passed to it. This is helpful when trying to debug whether you constructed your form correctly.

Contributors: Chad Humphries and matt.

Related changeset: http://dev.rubyonrails.org/changeset/8986

Make MimeResponds::Responder#any work without explicit types.

Here’s something I didn’t know: you can actually use any in your respond_to blocks as a catch-all response. E.g.

respond_to do |format|
  format.html do
    redirect_to :action => 'login'
  end
  format.any(:js, :xml) do
    request_http_basic_authentication 'Web Password'
  end
end

Contrary to its name, using ‘any’ actually requires you to pass a list of types to respond to. This has been enhanced now so that if you don’t pass any arguments, it’ll function as a real catch-all.

respond_to do |format|
  format.any do
    request_http_basic_authentication 'Web Password'
  end
end

Credit goes to Joshua Wehner for this patch.

Related changeset: http://dev.rubyonrails.org/changeset/8987

Add readonly option to has_many :through associations

Turns out the :readonly option for associations mentioned earlier was left out for has_many :through associations. This oversight has been fixed.

Thanks to Emilio Tagua for this bugfix.

Related changeset: http://dev.rubyonrails.org/changeset/8989

As usual, let me know of any inaccuracies or any suggestions you may have in the comments!

Living on the edge (of Rails) #10

Another awfully sleepy week on Rails edge. Though by the time I had sent over the notes to Gregg Pollack and Jason Seifer of the awesome Rails Envy podcast, there has been some nice changes (that I’ll be mentioning next week, but there’s absolutely nothing stopping you from checking those out yourself).

In other interesting news, Pratik Naik (lifofifo), a long-time Rails contributor has been given commit rights to Rails. Congrats Pratik, well-deserved and it has been long overdue in my opinion! Pratik keeps an interesting blog at http://m.onkey.org/ (where he’s not afraid to say “fuck” in his posts and his code) and hangs out an awful lot on #rubyonrails and #rails-contrib on IRC.

As a sidenote, Capistrano 2.2.0 was released by Jamis last week.

This week’s report covers changes from 25 Feb 2008 to 2 Mar 2008 (the day the corresponding Rails Envy podcast was recorded).

Time#end_of_XXX methods

A bunch of Time core extension methods have been added. These are: Time#end_of_day, Time#end_of_week, Time#end_of_year, and Time#end_of_quarter, which all return exactly what you expect them to return:

Time.now.end_of_week # => Sun Mar 09 00:00:00 0800 2008

Credit goes to Juanjo Bazán (a former Rails Hackfest winner) and Tarmo Tänav for contributing this patch.

Related changeset: http://dev.rubyonrails.org/changeset/8934

Date helpers now accept HTML options

ActionView’s date helpers (such as date_select, time_select, select_datetime) did not support any HTML options, unlike the other helpers (like f.text_field(:name, :class => 'my_css_class', :size => 20)). This inconsistency has been fixed and you can now finally do:

<%= date_select 'user', 'birthday', :order => [:day], :class => 'my_css_class' %>

Murray Steele (h-lame on the Rails Trac) and Jakob Skjerning contributed this patch.

Related changeset: http://dev.rubyonrails.org/changeset/8968

No need for explicit respond_to for RJS templates

ActionController has been changed so that JS requests will automatically render action.js.rjs files without the need to specify an explicit respond_to block. This means that your .rjs files work the same way as your .html.erb files - just put them in the right place and Rails will use it.

Related changeset: http://dev.rubyonrails.org/changeset/8956

Bugfixes

  • http://dev.rubyonrails.org/changeset/8937 - Prevent Rails from crashing when trying to deserialize an XML representation of a model named “Type” (using Hash#from_xml). Contributed by Juanjo Bazán and Isaac Feliu.
  • http://dev.rubyonrails.org/changeset/8942 - Fix eager loading so that it doesn’t pull in duplicate records in some cases. Contributed by Catfish.

As usual, let me know of any inaccuracies or any suggestions you may have in the comments!

Living on the edge (of Rails) #9 - the sleeper edition

It’s been a slow week on the Rails trunk this week in terms of exciting changes.

This week’s report covers changes from 18 Feb 2008 to 24 Feb 2008 (the day the corresponding Rails Envy podcast was recorded).

ActiveResource::Base now accepts user and password configuration

You can now set the user and password for HTTP authentication in your ActiveResource models:

class Person < ActiveResource::Base
  self.site = 'http://example.com/'
  self.user = 'konata'
  self.password = 'password'
end

This is a better way to specify authentication credentials than the current way of doing it by putting it all in the site (e.g. self.site = 'http://konata:password@example.com/').

Related changeset: http://dev.rubyonrails.org/changeset/8891

script/plugin install now supports SVN export

You can now pass the -e/--export option to script/plugin install to do an Subversion export of the plugin. This allows you to then check in the plugin’s files into your own repository. Though seriously, if anyone really wants to do this, I’d suggest they use Piston. That is, for those of us still using Subversion rather than Git.

Related changeset: http://dev.rubyonrails.org/changeset/8921

Bug fixes for associations preloading

There’ve been some bug fixes the associations preloading change mentioned in Living on the edge #5:

  • http://dev.rubyonrails.org/changeset/8894 - Fixed a bug where associations that were preloaded (using nested :includes) errors out when the associations return nil (i.e. when there are no associated records).
  • http://dev.rubyonrails.org/ticket/11154 - Fixed a bug where nested includes were assuming that all records are from the same class. This is not true for polymorphic associations, causing problems when they were being used.

Yup, that’s it for this week. As usual, let me know of anything I might have missed or any suggestions you may have in the comments!

Shokudo Japanese Food Bazaar (in Singapore) - don’t bother

Subscribers who read my blog for Ruby- or Mozilla-related posts should ignore this post, it’s another of those blogging as catharsis posts. To my defence, I haven’t done one of those for a really long time!

If you’re staying in Singapore and looking to try out the food at Shokudo Japanese Food Bazaar after it was featured in the local newspaper, my advice to you is, “don’t bother!” Why? In 2 words, horrible service.

Despite some positive reviews (yes, the decor is not too bad), my dining experience at Shokudo consisted mostly of getting the service crew’s attention (and failing spectacularly despite standing right at the counter) and being scowled at or avoided by most of the staff (which is not good, since being a bazaar-style restaurant, you needed to place your order at each stall manned by different members of the service crew).

My friends and I were speculating that either the staff got scolded that day (and reacted negatively to that), or they were just too stuck up because business was good. Either way, it was just terrible. When leaving, I even got rudely reminded that I’d left that one of those things they gave you to reserve a table. Did I mention the service was horrible? (Granted, there were 2 counters where the staff were attentive and actually quite friendly - this was at the macha drink stall and the katsu curry counter.)

What about the food? With such bad service I would say no one shouldn’t really care about the quality of the food - thankfully enough, the food is of a consistently mediocre level to even bother!

Has anyone had a similar experience at Shokudo, or was it just our unlucky day?