Archive for the ‘ruby’ Category

August 5

Beware of Rails Optimistic Locking and MySQL

Posted by mtoledo
Filed under rails, ruby | 7 Comments

There’s a caveat when using rails optimistic locking inside callbacks in a fault tolerant way with mysql’s default settings. Of course that’s a lot of things and sounds like a very specific scenario, but its not that much. Let me break it down:
Rails optimistic locking
Rails will automatically create locks around a record being updated in [...]

July 1

Watch out for using ActiveRecord’s update_attributes on dirty objects

Posted by mtoledo
Filed under rails, ruby | 2 Comments

I’ve recently found out a very odd particularity about how ActiveRecord behaves when relationship properties through the update_attributes method in ActiveRecord::Base. In fact due to its simple implementation, its actually a behavior of any saving of relationships on dirty records.

# in rails ActiveRecord::Base (base.rb)

# Updates all the attributes from the passed-in Hash and saves the [...]

May 21

Using scriptaculous’ Sortable and InPlaceEditor at the same time

Posted by mtoledo
Filed under javascript, ruby | 1 Comment

For a couple of days I’ve been trying to implement a scriptaculous’ InPlaceEditor on one of my pet project’s scriptaculous’ Sortable list.
First I had no luck with the easy to find InPlaceEditor rails helpers that you easily find on google: The traditional in_place_editing plugin required controller changes and didn’t support rest too well. Nakajima’s “better_edit_in_place” [...]

April 14

Why I don’t object to Array#sum

Posted by mtoledo
Filed under ruby | 7 Comments

Raganwald has written a very elaborate piece about his “objection to Array#sum“. Since I patircularly like Array#sum, I thought I might weight in on my own blog.
First, I think that his piece has 2 different objections:
1) Array shouldn’t return true for a “responds_to? :sum” call if it has contents that can’t be summed ( like [...]

April 9

On caching expensive case conditions

Posted by mtoledo
Filed under ruby | No Comments

Ruby has a pretty expressive and flexible case statement. It actually differs from most other language’s ’switch’ statements in that it auto-breaks on each condition instead of falling through.

// C

switch (x) {
case 1:
doSomething();
break;
case 2:
doSomethingElse();
break;
}

# ruby

case x
when 1: do_something
when 2: do_something_else
end

I do think not having to break was the right decision, but not everyone agrees and [...]

April 8

Using ‘map’ effectively on ruby Hashes

Posted by mtoledo
Filed under ruby | 2 Comments

Ruby is a very powerful language, and the methods available to manipulate its 2 main data structures, Array and Hash, are really good. Though, some of them are really obscure, and for some other manipulations you are on your own. This happens specially with the Hash class.
To me, this is probably because although both Array [...]