Page 15 of 42

SOAP calls make activerecord stop working, oh my

Create an active record object, retrieve some stuff from a soap service and then get this:

NoMethodError: You have a nil object when you did’t expect it!
The error occurred while evaluating nil.has_key?
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:2172:in `has_attribute?’
 

If you’re having trouble with active record suddenly throwing exceptions it’s probably because of a namespace collision with your Soap classes defined in the generated file called default.rb, or YourClassName.rb depending upon what arguments you gave to the wsdl2ruby command.

You need to put the data transfer classes into their own module by adding

—module_path MySoap

to the end of the command line. So you have something like this:

wsdl2ruby.rb —wsdl my.wsdl —type client —classdef My —module_path MySoap 

(remember to delete the old files!) 

I last fixed this by hand editing the files and adding in the module definition – so this is by far the easier way.  

Another problem is you get this:

NameError: uninitialized constant SOAP::Mapping::EncodedRegistry 

Just put

gem ‘soap4r’

in the mapping registry file 

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? }

Presentations

Comment left  

Eschew bullet points – they are there to prompt the speaker
Use diagrams and pictures – people think in pictures
Give handouts – That’s where screeds of text belongs.

I read a paper that says one of the shuttle disasters was indirectly caused by powerpoint bullet points. A key engineering point about checking for damage was hidden in a 6 point font on a slide about 15 slides in. The engineering company was ruled by salesmen and the only way they could communicate was using it. Instead of producing a proper technical report that everyone could read and discuss at a proper meeting NASA were handed a BS powerpoint full of tiny fonts and missed this – it cost a lot of lives and money. I hate bullet points.

Also, the font size should be half the age of your target audience – another reason to use pictures.

Back to the Dharma

Recontacted my Dharma teacher last year after a long time of being unable to practise. It was because I was trying too hard and strained myself. He told me that it’s very common for westerners to do this: we’re taught by our education system that we have to be perfect and whatnot, instead of aspiring to be like the buddha and working with what we have at hand we think we have to be the buddha right now and either give up because it’s impossible or do ourselves an injury.

I’m very happy now, have been meditation regularly again, but this time not trying to sit beyond a reasonable amount. Also studying again. I’m trying very hard to write things down and systemetise them this time using things like mind maps. Ultimately it does’t matter much, but I think it would be good to be able to remember things like the Four Noble Truths, and the four thoughts that turn the mind to Dharma, without having to get a book out. Memorising some of this stuff is good, it will give me a grounding to understand other things better.

I tend to jump off to the complex shiny stuff (Buddha nature, the madhyamaka etc.) because it’s interesting and way beyond the everday view of things. Then details trip me up and I realise the shiny stuff is’t that important. So I’m trying to get a firm grounding in the key concepts and ideas. This should stop me falling off the end of the pier again. The spiritual consequences of leaving Dharma are extremely severe, particularly if you’ve entered the vajrayana.

Blessings to you all. 

Office 2007 Adoption Barriers

Posted here. 

I’ve been using it for a while now, moved because of incompatibility issues.

Perhaps the complete redesign of the interface? Try finding “save as” in Excel – the File menu no longer exists FFS! It is there, but nowhere obvious.

Perhaps the “ribbo” that takes up half your screen if you’re on a low-res machine?

Perhaps the redesign of formatting in Word, which was way better than Open Office, to be almost unusable and no longer number headings properly?

I think XP was the terminal release.

I also read that MS have got rid of a lot of their testers and gone in for automated testing of everything – so the whole usability thing they spent billions on has gone out of the door.

Open Office is more compatible with XP than Office 2007 – wtf were they thinking? I recon OO will really take off now.

Thanks, guys, for wasting my time. Can I send you a bill?