Month: February 2008

SQL is dead?

Comment left here. 

XML – sigh

I read Oracle’s early papers on XML DB – “we store the schema and the data separately, with pointers into the data”

Codasyl in disguise. The same with the o-o databases, hierarchical databases – none of this is new at all.

Personally I can write SQL, I understand the syntax and have been using it for over 20 years. Once you realise you’re really working with sets it’s pretty easy to understand.

RDBMS is now just another commodity. I’m happy to use whatever comes next, I currently develop in Ruby on Rails / MySQL and hardly ever have to see the database directly. Interestingly, the people who designed Active Record made 90% stuff really easy and then allow you to drop into raw SQL for the complex, instead of retrofitting the kitchen sink.

I’ll use whatever comes down the pipe, as long as it meets my needs – that’s it.

Deep merge a Ruby hash, the joys of recursion

I was doing some hacking with lists of valid phone number codes (3,4,5 digits depending upon various factors). I used hashes that could be walked down because of the key lengths. Now we’ve decided to do it  differently because the data file was’t up to date …

This gets the phone number as an array of integers:

      code_pieces = code.to_s.split(//).collect(&:to_i) rescue []

(note the Rails &: thing)

This takes a list of keys and a value, pops the value off the beginning of the array and when the array is done then puts the value in with a hash key of -1:

    def hash_for_digits(n_array,value)
      n = n_array.delete_at(0)
      if n
        the_hash = {}
        the_hash.merge({ n => hash_for_digits(n_array,value) })
      else
        { -1 => value}
      end
    end

This is a merge of a hash of hashes with another – warning – it blows up  without the -1 markers from the previous function.

    def deep_merge_hash(hash1,hash2)
       hash2.each_key do |k1|
        if hash1.key?(k1)
          deep_merge_hash(hash1[k1],hash2[k1])
        else
          hash1[k1] = hash2[k1]
        end
      end
    end
 

Hope others find this some use – I might return to this another day and make it work with arbitrary hashes but now too busy and did’t want to throw the code away … 

The return from the function looked like this:

      top_number = get_code_ref[code_pieces0][code_pieces1][code_pieces2] rescue nil
      if top_number
        ok = top_number[code_pieces3][code_pieces4][-1] rescue nil
        ok = top_number[code_pieces3][-1] unless ok rescue nil
        ok = top_number[-1] unless ok rescue nil
      end
      ok

I realised I could probably have speeded things up by having the first 3 as a hash key in their own right but too late now – reimplementation with a different data set on the way … 

Global Warming – the problem with it

Insightful few paragraphs here.  

If John and others are right, the climate change folks look like tools, if they are wrong we might be in deep trouble.

That’s the problem.

That said, I agree that we can overcome difficulties with technology and ingenuity, and the Green fascist types (who want at least 60% of the human race dead and the rest living on cardboard) tend to see the world as static and unchanging don’t get this. But then the whole racist Malthusian project always becomes fashionable when the economy’s in trouble. It’s quite likely that global warming will become another one of their arguments for sterilising people who happen to have a skin colour they don’t like.

I was sent a “reasons to go veggie” leaflet by someone who seemed to think that forcing subsistence farmers to grow and eat what vegetarians think is a good idea is ok. Another complacent fat westerner telling people what to do and what they can and can’t eat. It was sickening, and the author of the pamphlet did’t even realise how racist it was. I think the global warming thing is going the same way – there’s a lot of anti-Chinese and Indian sub currents when you read the articles. Interesting, eh?

Labelling young people – sigh

Response to one of the comments left here by Stuart Luscombe  

“when I was a teenager a mere 10 years ago there just were’t the kind of problems you keep seeing today. If anything I think school’s, parents and the law in general is still too soft on young people who think that because of their age they can get away with anything they like.”

I left school in the late 70’s with 7 ‘O’ levels and a broken nose. Stuff just did’t get reported then. If anything things are better now because schools at least try to deal with bullying, even if they sometimes fail.

My wife is a Guide leader, and I also used do a lot of work helping her and friends who run scouts. Most youngsters are decent, hard working folk just like their parents. They dislike the chav scallywags as much as you obviously do, we’re talking about maybe 5% of the population here, but they get the headlines because it suits our lords and masters to demonise them and frighten the rest of us.

Stuff gets into the Daily Mail because it does’t happen very often – if it were happening all the time then it would’t be news, d’oh?

Get a grip.

Strange error with ActiveRecord in Rails

 

I was getting this:

undefined method `to_f’ for {}:HashWithIndifferentAccess

This was happening in the after_create method. We have a table that lists fields and what validation is needed, we use this to pick out the value in the record so we can apply the validation function to the value if one has been given.

field_value = self.send(field_name) # Error was thrown here

I fixed this by calling self.reload at the beginning of the method that does the field scanning. I think that ActiveRecord does’t refresh its attributes hash after save and the error came from it expecting the value to be of a particular type… I don’t like calling reload, but can’t find a method that will refresh the attributes hash.

Bootnote

Discovered that this was the result of sending nils in the hash passed into the create method, as it came over the wire as XML and was then incorporated into the params array, unlike the conventional post method, which puts empty strings in there. I wrote a one-liner to remove the hash elements with nil in them and everything started working. There was also another nasty bug which was putting the HashWithIndifferentAccess thang into any empty strings.

params.delete_if{ |k,v|  v.blank? }