Category Archives: ruby

Be sure where your methods are chained to…

I’ve just figured out a quite obscure bug in our app. It all started like this:

record.freeze.things # record is an ActiveRecord, and "things" is an association on that record.
TypeError: can't modify frozen object
	from (irb):2:in `instance_variable_set'
	from (irb):2

The code above shouldn’t crash, because ActiveRecord hast its own #freeze method, which will still allow access to the associations. But our record behaved as if Object#freeze had been called on it. What happend?

Continue reading

Rails forms and params

Another not to self: Rails has the cool feature that you it will map all the values from your submitted forms into the params hash. And, if you create input fields that are named “something[bla]“, it will automatically create a nested hash so that you will be able to access params[:thingyform][:something].

But what if you just have a list of things that you need to submit? In this case, just name multiple form fields something[], and the params will contain an array.

Assit on GitHub – the gem is back

I while ago I wrote a small ruby library for runtime assertions to use in our projects. While I didn’t use it as heavily as expected, it has been useful in debugging in the beginning. It offers the possibility to include extra runtime checks – even expensive ones – to the code, which can be disabled in production code.

I’ve moved the project to github now, and the gem is not broken anymore. This means that you can

sudo gem install averell23-assit

from gems.github.com. The old gem (assit) is still around on rubyforge for some reason, but it’ll remain on 0.0.3 forever. If you use this, better get the github version (averell23-assit) now.