Over the past week or two I’ve noticed traffic to my Blog have dropped dramatically. Upon further investigation I have found that traffic to one of the most popular articles "Ruby and Rails Naming Conventions" have almost dropped to zero.
I’ve checked Google and the other search engines and this article does not appear at all when using the following search terms "rails naming conventions", it use to be one of the top five for quite some time.
I have searched the Internet to try and figure out why my article is no longer listed, but as yet I’ve not found anything that may explain why this has happened. If anyone has any ideas it would be much appreciated if you could let me know. I’m not sure if I can approach Google about this as this seems to be the same for all search engines.
Click on this link to access the actual article Ruby and Rails Naming Conventions
When a database field in a table is NULL and this field is then used in a view a nil object exception will be raised. So for example if I had the following code to display the name of the user in my view and the login field was NULL an error would be generated as login object would be nil.
Signed in as <%= current_user.login %>
To overcome this you would need to use something like the code below:
Signed in as <%= current_user.login unless current_user.login.blank? %>
Which if you have many field to deal with becomes a major pain. As I was reading about this I realised that nil in Ruby is an actual object that has methods including to_s which will return an empty string when called. So instead I can do the following:
Signed in as <%= current_user.login.to_s %>
When the login field is NULL a nil object will be returned and the to_s message sent to this object which in turn will return an empty string, no error messages will be generted and everything works nicely.
I’m currently writing an article on Ruby’s approach to objects, classes and also the use of self. Coming from a C++ background it has taken me sometime to grasp the Ruby way of doing things.
I’m doing it for a number of reasons, I want to enforce in my own mind the things that I have been learning, I find writing them down helps a lot. I also want to use it as a reference in the future as I’m sure I’ll forgot things down the track, also someone else may find it useful.
So look for Part 1 in a few days.