Query SQLite looking for boolean records

rubyToday I have needed to ask an SQLite DB to get some records with a field set to true, and all my first attempts to do it have been failed. Finally I have achieved it, and I would like to share this little achievement :)

NOT working:

Model.find(:all, :conditions => 'field = true')

NOT working:

Model.find(:all, :conditions => 'field = "true"')

NOT working:

Model.find(:all, :conditions => 'field = 1')

NOT working:

Model.find(:all, :conditions => 'field = "1"')

and finally working:

Model.find(:all, :conditions => 'field = "t"')

The solution has consisted on looking at DB level, which is the real data stored on it. Cheers :)

Posted in Programación | Tagged , , , , , , , | Leave a comment

Dinamyc callbacks on Rails, is it possible?

Today I have tried to do this: let any application application, and among all the tables to storage the data of your model estructure, there is one called “Configuration”. As you can suspect, this table will storage some configuration options for your application. For some configuration options you need to execute certain code, and here is what I’ve tried:

class Configuration < ActiveRecord::Base
# These next lines are related to Hobo gem, and are just 
# an expression to define the model (you can imagine what they do for sure)
  fields do
    name :string
    value :string
    callback :boolean, default => false
    callback_name :string

after_update self.callback_name.to_sym, :if => self.callback?
... here comes the definition of each callback ...

For some reason this is not allowed. I think this is a nice solution to hook configuration options within code, but it doesn’t seem to work :/

If any of you get something like this working, please let me know, I will own a beer (no alcohol obviously :)

Cheers, Ibon.

Posted in Programación | Tagged , , | Leave a comment

Capybara screenshot on ancient Rails 2.3.X projects

Recently I’ve been playing with some tests on an old Rails Project. In terms of actual Computer Science Mesure Units or CSMUs, this is quite old (remember that one CSMU is the time used by a user to change his/hers iPhone :)

Some failures on the test forced me to look up for some solutions and Capybara-screenshot was one of the elected ones, instead of being playing around with messages on the log file. The sad side of recent versions of this gem is that it seemed not to play well with Rails 2.3 infrastructure. There is a Rails 2.3 compatible version but some of the usual methods of Capybara-screenshot were not working well.

What I have founded is this recipe running for me: install capybara-screenshot 0.3.14, then require it on your spec_helper.rb file. When your test fails the screenshot is not being automatically taken (at least in my case), but you can use it manually, before the offending line by putting “screenshot_and_open_image” :)


Posted in Programación | Tagged , , , | Leave a comment

Si no conoces Duolingo, deberías :)

Mas vale un enlace que mil palabras :D

Para solucionar posibles problemas con el micro en Ubuntu, necesitas instalar (para Gnome):

sudo apt-get install adobe-flash-properties-gtk

o (para KDE):

sudo apt-get install adobe-flash-properties-kde

y luego lanzar Adobe Flash Properties o (en un terminal):


Salud ;)

Posted in Formación, Ubuntu | Tagged | Leave a comment

Amigo invisible

Este año vamos algo justos para el reparto de papeletas del amigo invisible, así que una pequeña contribución para la generación automática de parejas (en Ruby):

# Preparamos las posibilidades de elección y las personas que eligen
posibilidades = ['marta', 'maria', 'mikel']
humanos = posibilidades.clone
asignacion = []

for h in humanos
  # Si me ha tocado mi propio nombre, vuelvo a cojer una 
  # papeleta hasta que me salga otro nombre que no sea el mio
  aun_no = true
  while aun_no
    i = rand(0..posibilidades.size-1)
    p = posibilidades[i]
    if h != p || posibilidades.size == 1
      # ¡ya tenemos una pareja!
      asignacion << {h => p}
      aun_no = false

# Mostramos las parejas por pantalla
for a in asignacion
  a.each {|key,value| 
    key != value ? puts("A #{key} le ha tocado #{value}") : puts("Oh, a #{key} le ha tocado a si mismo")

Posted in Ubuntu | Tagged , | Leave a comment