Techblog

Tech Blog

Our latest geek adventures!

Archive for July, 2007

24 July Tutorial: Gettext for Rails in 8 steps

Posted by jaap in Ruby on Rails

Localization is easy with Gettext and Ruby on Rails. In this tutorial I’m going to describe from top which steps are needed to install gettext in your Rails application. We shall start with an empty Rails application and add Gettext to it.

Read the rest of this entry »

8 Comments -

19 July Rethinking Design Patterns

Posted by Gert-Jan in Development

Jeff Atwood recently wrote a very interesting post about this topic. He states that:

  • Design patterns are a form of complexity. As with all complexity, I’d rather see developers focus on simpler solutions before going straight to a complex recipe of design patterns.
  • If you find yourself frequently writing a bunch of boilerplate design pattern code to deal with a “recurring design problem”, that’s not good engineering– it’s a sign that your language is fundamentally broken.

Check out the whole story here: Rethinking Design Patterns

No Comments - Tags:

17 July Rails ActionWebservice :: Type casting

Posted by jaap in Ruby on Rails

Today we started the development of a Rails API. The first thing we had to do was defining a cast from a model to a Struct, cause we didn’t want the API to send all the fields we have in some tables.
In the rails documentation I could only find that it was possible to hide fields from your models, but not how. This is how we did it. We included the defined the models as a base class ActionWebService::Struct within our Api class. Example:

Let’s say you have a model User (who doesn’t) which has as base class ActiceRecord::Base. Now we want to only show, let’s say the field email.
This is our API for now:

class SomeApi < ActionWebService::API::Base
class User < ActionWebService::Struct
member :email, :string
end
 
api_method :get_user,
:returns => [User]
 
end

Now the User model with 20 fields will only be casted through a Struct and sent through the webservice with only the field email. That’s what we wanted!

No Comments -

16 July Slow Rails in production mode due to Gems

Posted by jaap in Ruby on Rails

Last week I decided to tune the performance of our Rails applications, because I had suspicions the app performed not at it’s best.
I installed a fresh Rails application and started the simple script/server -e production. I did the command: ab -c 5 -n 1000 http://localhost:3000/ and found out that we had only 396 requests per second. On my own, slower laptop, I had 500 requests per second for a test app of the same Rails version. Why o why?

Decided to take a look at the Gems. I saw that Gem installed a new version on top of an old version, when I updated. These old versions were still used in our production application, while these new versions were installed! You can specify your GEM version in the Rails app, but if you haven’t done this, Rails will apparently take the oldest version. After removing these old versions I benchmarked the server again, and now 560 request per second, ow yeah!

So an advice to all of you, figuring out why rails doesn’t perform at it’s best: update your gems AND remove the old versions. Now we updated our real applications and they are running lots faster.

No Comments - Tags: , ,

11 July Multiple relations between 2 tables in Rails

Just a small post about something I figured out recently…

If 2 database tables have a relation, in Rails you model it using the belongs_to and the has_one (has_many) attributes. Rails guesses the foreign key based on the names of the tables and its field names. In my setup I had 4 relations between 2 tables, so Rails couldn’t guess the foreign key’s by itself. All I had to do to make it work, was to specify the foreign keys and the related class:

  belongs_to :var_1, :foreign_key => "id_1", :class_name => "RelatedClass"
  belongs_to :var_2, :foreign_key => "id_2", :class_name => "RelatedClass"
  belongs_to :var_3, :foreign_key => "id_3", :class_name => "RelatedClass"
  belongs_to :var_4, :foreign_key => "id_4", :class_name => "RelatedClass

1 Comment -