Friday, February 24, 2012

Validating Virtual Attributes in Rails 3.1

When validating virtual attributes in Rails it's important to remember that they are just that - virtual attributes. I spent almost an hour last night trying to figure out why my validations were throwing an error when I called Object.valid? My class was structured similar to the following (forgive me, I am a Rails n00b):
Class User

attr_accessor :password

validates :password,
:presence => true,
:on => create

validates :password,
:length => { :minimum => 6 :maximum => 8 },
:if => :password?
When calling valid? on the User object I got a method undefined error for password?. I could't quite figure it out and I was wondering what I was doing wrong until eventually I tried appending the following to the bottom of the class definition:
def password?
An seemingly simple and obvious solution, I know, but this took me about an hour to figure out after Stack Overflow failed me for the first time.

I hope this helps save someone else the frustration I endured.

Thursday, February 23, 2012

Fixing Ubuntu Issues: Ugly Font Rendering in NetBeans

Recently i've had the displeasure of working in NetBeans on Ubuntu. The IDE is great but the font rendering is terrible. Here's my solution to this problem.

The first step is to uninstall Open JDK (if it is installed) and install Sun's JRE. Do that by selecting the following packages in the synaptic package manager:

  • sun-java6-bin
  • sun-java6-jdk
  • sun-java6-jre

Mark all installed instances of Open JDk for uninstallation if you have to, then click Apply.

Confirm your Java version by going to the console and entering

java -version
You should see
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Server VM (build 20.1-b02, mixed mode)
Now that that's done, install Microsoft's Core Fonts. Do this by going to the Ubuntu Software Center and searching for 
Install them, then open NetBeans, go to Tools > Options > Fonts and Colours and choose the Courier New font. I set mine to size 18 bold. Here is my result:



Wednesday, February 22, 2012

Fixing Ubuntu Issues: Restart Wireless Without Reboot

Sometimes after resuming from sleep, you would find that your wireless connection no longer works. Up until now, my only solution to this was to reboot. I've found an alternative solution from user mariosx on that involves simply restarting the wireless interface. To do so, enter the following in a terminal window

sudo ifconfig eth1 down
sudo ifconfig eth1 up

Where eth1 = your network interface. Could be wlan0 or something like that. I have not yet tried this out, but it's one of the more sane solutions i've come accross for this problem during my searches.

Fixing Ubuntu Issues: Flickering Pointer

Upon instlaling Ubuntu 11.10 some of may notice that the cursor constantly flickers and sometimes disappears randomly. To solve this do the following:

Note: xorg.conf is depreciated in Ubuntu 11+

Type the following in a new console window:

cd /usr/share/X11/xorg.conf.d

sudo touch 10-monitor.conf

sudo gedit 10-monitor.conf

Paste the following into the file, changing variables where indicated by the comments

Section "Monitor"
    Identifier    "Monitor0"

Section "Device"
    Identifier    "Device0"
    Driver        "nvidia" #Choose the driver used for this monitor

Section "Screen"
    Identifier    "Screen0"  #Collapse Monitor and Device section to Screen section
    Device        "Device0"
    Monitor       "Monitor0"
    DefaultDepth  24 #Choose the depth (16||24)
    SubSection "Display"
        Depth     24
        Modes     "1680x1050" #Choose the resolution

Save the file.

Press CTRL + ALT + FI to enter terminal mode and type the following:

sudo stop lightdm

sudo start lightdm

Log in and notice that the cursor does not flicker any more. You may have to unplug your mouse and plug in back in to get the cursor to work. If you do not get back to the graphical mode after starting the display manager, press CTRL + ALT + F7