Author: francis

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?

Debugging Rails Applications

I’ve said that I use netbeans on this blog a few times but never really gone into why.

First, I can park all of the class/project browsers and output windows in the margins of the page and they only pop-up when I click on them.

Second, I can tear off editing windows so I can look at, e.g. Rspec’s and the code next to each other. I can also have multiple views of the same file.

Third, it has book marks, which the “poor ma’s Textmate”, ‘e’, does’t.

Fourth, ^B will take me to where a function is defined so I can look at the framework and work things out. ^K is a cool key too.

Fifth, I can map everything to key presses if I want.

Sixth, it does indentation properly (which the Eclipse-based Ruby environments don’t and it annoys me too much to use them) – it will also re-indent at a key press, which Textmate does’t seem to do.

Seventh, I can move and copy lines without having to use the mouse: Ctrl-Down copies the current line or selection, Alt-Shift-Up/Down moves the current line or selected lines in the direction you need. This is great. Plus rename of variables inside a method …

Eighth, you can start mongrel inside the IDE with a keystroke, or start it against the current page.

But best of all, it allows me to debug my Rails apps. You set your break points, start up the debug version of Mongrel and it drops you into a full symbolic debugger. I had’t realised that lots of people have’t got this facility until I heard a podcast from David HH, the inventor of Rails, when he was saying that debug is back in Rails 2.0. If you use Netbeans it never went away, and you don’t have to put breaks in your code.

Plus all the generators and whatnot are visible in the GUI.

The learning curve is’t flat, but not very steep, and it’s a good tool that runs on any platform that supports a Java VM.

Enjoy 

1972: Dead teachers don’t lie

What follows is an extract from my novel Archive Fragments. 

There were great tensions in society back then; tensions that people do’t remember now. They talk about class and ethnicity as if they were new phenomena. But I lived in a country that tolerated Jews in a leery, cautious way. It had been nominally Christian and definitely white for over a thousand years. Some schools were still tied to churches – I even went to one when I was a junior. Many bad things happened there, but that is for another time.

Secondary school there was’t a single black face in the place. This might seem strange now, but then it was’t. Our government invited people who were citizens of the old empire to come and work, to come and do the low-paid jobs that they could’t find people for. They were easy to spot, they were mostly descended from former slaves, they had black skin, and non-UK accents. The unions quietly forgot their internationalist leanings and started to stoke up racism against the threat of lower-paid workers damaging differences in pay. I remember a lot of gibberish about differentials in the news at the time, with my child’s mind, and now look back to see this racist agenda. Of course, scratch and Englishman and you find a racist, because Englishness is defined in terms of who ca’t join the exclusive club. I do’t care if that statement annoys you, sorry.

So, it was a little odd when a black child appeared in our school. He was the only one and did’t have a good time of it. Most of us were’t against him, most of us did’t have an opinion, but of course, a large school with several hundred kids in a year and fifty or so teachers had its fair share of dickheads, ignoramuses and out and out bullies.

There was a class distinction between the teachers. There were the arts types, the elite who had gone to a proper university before the blurring of institutions that has happened since, physical education teachers who did a bit and were mostly psychotic ruffians, and the failed engineers who taught technical drawing and metal- or woodwork. This last bunch were old-school and had probably been teenagers at the end of the War.

I have an abiding memory of this poor child standing on one of the woodwork benches, where a bald bastard of a teacher and a slightly older army veteran type were abusing him and using words that you never hear any more. This offended me deeply but I sinned by not taking a stand and getting someone in authority to confront them, if anyone in authority had the balls, which I doubt.

The kid only lasted about a week and I never saw him again. I hope he survived and wish I had been able to do something for him. I’m not sure I even spoke to him; the bullies would have gone for me as well if I had. This shames me.

The next school year it was announced that the bald bastard had died of a heart attack over the summer. I remember the headmaster looking sad, and we were all supposed to be a little sorry. I was glad, it felt like justice, but the older teacher who had egged him on was still there. Of course, he had a family and there was a whole complex web of lives and relationships around him and it was sad, like it always is when someone dies young. But I was’t sorry because the thing that defined him for me was that child standing on the bench, bravely holding back his tears.

Now I am an adult I will not tolerate racism or bullying, and I will not be quiet about it either. So hard luck if you stand someone on a table and abuse them – I wo’t be sad or sorry when you leave and I will make sure you do…

Fun with openssl on Ubuntu and Ruby 1.8.6

We migrated a core system to our shiny new cluster on Friday. Colin got everything going apart from a call-out to a 3rd party back-end system that uses SOAP.

Tracking down the problem:

The error message was a method_missing talking about load_file.

I did some google work (googling is apparently not allowed by google because it could undermine their copyright – like I care)

It looked like the call to the SOAP client to add the certificates file was’t working … however the permissions on the files were correct.

I had a look on Sunday and found that we were missing the Ruby openssl library. This is’t a gem, but part of the standard install. Found this link explaining how to sort it out. Looks like we’re missing zlib and some other stuff too.

Interestingly one leg had readline installed and the other did’t, which meant that the console would’t work. I could’t get it to build on the broken leg and resorted to sftp’ing the file from one machine to the other.

This is because you can’t get above Ruby 1.8.3 using the apt-get command so you have to build it from source. No idea why it does’t install standard stuff – there’s probably some magic compile switch that is’t documented.

Have fun. 

Joel hits the nail on the head again

Joel does it agai 

Part 1: http://www.joelonsoftware.com/items/2007/12/03.html
Part 2: http://www.joelonsoftware.com/items/2007/12/04.html
Part 3: http://www.joelonsoftware.com/items/2007/12/05.html

Posted o reddit. 

I’ve been in the game since 1985 and there was only really the “in house programmer” type job in those days, usually using COBOL. Have managed to work for a couple of software houses since then and I agree – it’s a much happier experience.

Automation – yeah, I agree, you can’t automate sexiness or ease of use. A lot of techies have a very good data model (or did before incoherent Java object models became de facto) and then just generate a pile of unusable crap from it, which is logically complete but very hostile. Cost cost cost cost – when I worked for Oracle they had a tool that pretended to do “100% generatio”, crap crap crap crap. But allegedly cheaper.

Good stuff Joel, keep it up.

Coding Horror: Presentation: Be Vain

http://www.codinghorror.com/blog/archives/001007.html?r=21636#endcomments

I spent most of my spare time from age 8 to 16 learning to play the guitar and these instant gratification things just annoy me.

That said, I agree with the point that presentation is far more important that people think, and the example is a reasonable one. Please stop focusing on the specifics here – Jeff’s trying to make a very reasonable point.

Techies start from a data model and then wrap it with some half-assed forms from a generator – I know we do, I’ve done it myself lots. I learned that this does’t work because the logical view of the data isn’t the user’s view. Not listening to their pain and trying to help them get to where they want is arrogance. For example, on a system our users use (and I did’t write) they hate the way it takes forever to open the person details screen and all they ever want is the email address or phone number – a techie generated the user form out from the data model and joined it to the logically correct place. Imagine how much extra effort it would have been to put the phone number and email address on the summary? Hardly any, but it does’t fit the cod-head approach. I would lay money the coder has never talked to a user or watched one use the system.

The sexy unwritten thing is’t at all new. I worked on many projects in the pre-web days where the client had “bought the powerpoint”. Then they kicked us lots because it was all a lie.