Wow, its been a long time since I wrote anything. I guess I haven’t done too much cool lately in programming or really much coding to think of it. But I have been working on some things…

Android! — I’ve been working on an android app with a friend … soon we can talk about it I think. Its kinda fun to program and actually see it on a device. I’ve been bored with web programming, so this is actually something different and interesting.

School! — I’m starting a masters program in computer science. First class? … Java … Though I am not superexcited about java, it what I need at work now. SO, java it is. I took my ruby books off my desk so I wouldn’t have to be reminded of them. ::sniff:: ::sniff:: (Don’t slap me Obie!)

Haskell! — Yeah, I’m learning Haskell … I am reading Real World Haskell which is available free but I also ordered the book. Nothing like flipping through pages of a book… but its also nice to have it online too! I’ve always “heard” it makes you think differently and so I am looking forward to that since I’ve been bored with programming really the past 6 months.

Well, thats all for now … look for some tidbits about haskell and android in upcoming weeks.

I am not not not not doing it! I am not getting an iphone. The lower price is very very tempting… but I am not doing it. A friend of mine signed up for an apple developer account like hours after it opened — I asked him yesterday if he was approved and he said ONLY 4 PEOPLE ARE! out of 200k … huh? what? where are all the HUNDREDS of apps that Apple thought would magically appear when they let the SDK loose on eager developers. Hello?

What is going on Apple? I love my mac… and would like to have a phone that would integrate with it ….

Meanwhile I haven’t done too much with Andriod development but I keep up reading blogs about it. I don’t feel like I have all the restrictions and “Steve Jobs says …”

I’m holding out for an android phone…and by the rate things are going with the iphone developers.. my app will be done before yours.

I had a pcmcia sierra wireless broadband card for my old laptop for the past 2 years , since I have a loooong commute and its just plain boring being off the grid. I am usually programming on the go and sometimes I need to look something up or ask a friend or install a library. On one hand, it was a distraction. I would get on the train and just browse the web instead of concentrating on a project. On the old laptop i dual booted - so when in linux, no internet, I could not set it up for the life of me. So I had to boot to windows (another reason to NOT use the net, ugh windows). So, I could get alot done without the distraction. Well, I want internet back!! even so much so that I am not getting a crackberry or iphone or any other fancy phone for awhile (secretly holding out for a android phone in the fall)

So I am happy to say — the new aircard is on the way!

Being off the grid I found “offline” things to do….

Google Reader — offline mode is freaking awesome. Before i log out at night I sync and download my blogs. Read them on the train, get to work - sync and download. If I remember I will sync and download them again before I leave to get the afternoon content. Today for some reason, its not working. :(

Print to PDF — if I find a page with a blog, tutorial, whatnot I will print to pdf to read it offline. Its pretty cool. I can search it easy too. Generally webpages look decent, though I’ve had some sites where the text runs off the edge of the page into who knows where?

Stockup on PDF books — This has more to it then just being off the grid, but for the last Apress book review I am doing I asked for a PDF book. Nice, I got it within an hour! Rock on. I still like paper books though, for home. And for reading at the gym.

Sleep — usually I can never sleep on train and usually never ever nap unless I am deathly sick. If I skip the after lunch/afternoon coffee I can sleep on the train usually. I usually have to set my backpack on my lap soI have something to lean over on. My rain poncho that folds up into a little pocket actually makes a nice pillow! I don’t sleep every day, but every so often. I haven’t had much success sleeping in the mornings, even if I dont bring coffee with me.

Read paper books — obviously… I’ve been reading some fantasy books: Warcraft Archives, its fun to read about these places and people i hear about in the game of World of Warcraft. I’m also reading Elminster, a book about a wizzard. Its pretty cool.

Playing DS — some days I bring my Nitendo DS with me. I am working through thew New Zelda game and Final Fantasy III. Boy is FF hard, I get so far then die…and I have to start over. Where’s the freaking save point? Its the first time I have played a game of this type, my husband warned me it was hard core and difficult! But I feel up to the challenge.

Write Blogs — I’ve been writing more lately I think, I will write them offline then upload them when I get to a net connection. I will sometimes even compose emails into a text file to people and then copy-paste them to emails when I get net.

I’m watching the Git peepcode by Geoffrey Grosenbach and as usual with other peepcode it is very good. This is the ultimate in geek-tv. I swear, i will burn these to a DVD sometime so i can watch them on my tv! :)

Things I like about git so far:

- branching, since I am often on the road without internet access.
- clean, just creates one .git dir in your project root. I just found a bash command that will clean out my .svn dirs from a project I’ve modified so much on the road that I’ve borked up the svn process and I’m going to convert it to git

BTW here’s that bash command if you are interested

from: http://www.anyexample.com/linux_bsd/bash/recursively_delete__svn_directories.xml

its simply:

rm -rf `find . -type d -name .svn`

Haha, I remember I wrote a perl script I think to do this like 4 years ago for someone. Who knew it was just a rather short bash command…apparently not me.

Branching is freaking awesome. But what is even more interesting is the stash. Geoffery said it was like a clipboard. You can be working away and you have some changes you are in the middle of but you don’t want to commit yet, so you stash them, then go do something else, then later get that stash and apply to your code. Interesting.

There is a progam gitk that will give you a GUI look at your branches and commits. Thats cool.

get-svn lets you use git locally and then check back into svn.

BTW I keep typing get instead of git! … grrr.. I am about ready to make an alias! :)

I like! .. and the Peepcode screencast is great, as usual. I will need to watch the branching again but I highly recommend getting this screencast if you are interested in learning git. Its only 9 bucks!

I’ve been doing a variety of things lately:

Playing with Android
I got the “Hello Android” beta book from Pragmatic Programmers — I am trying to remember its beta but its sort of hard to follow. Be prepared to download the source code for the book and compare to what the book has to see what is missing, so you can get your thing to run. I am going to look at some other tutorials at the moment to get more familiar. Maybe the author assumes the reader knows more than me! Which is possible since I haven’t done much java programming.

Wordpress 2.5.1 and EngineYard

The DevChix.com blog is a wordpress blog and hosted at DreamHost. We got a free slice for DevChix from the kind folks at EngineYard. They were nice enough to set it all up for us with wordpress and giving us a deploy.rb file ready to go! Their support is pretty fast too and I was able to get some help from the irc chat as well. The new wordpress has some neat features and looks nicer. I want to install a captcha though for blog comments, even with Akesmet I get tired of sorting through them! I was able to get all our plugins installed and seems good.

Playing with App Engine
I worked on this last week while on an airplane. I wasn’t sure it would work offline — but it did. Even the database…err..yeah whatever it is. I was able to add data ..but where is that data?

Django your mango
Yes, been checking it by doing the bundled tutorials. I haven’t gotten back to it since my last post. Need to get back to it before I forget what I was doing. haha.

Git ‘er done
Finally, I am going to learn git and try it on a project with a friend. I am hoping it proves useful since I often code on the go with no internet access. I sometimes have to do a poor mans cvs and save things as myfile1.rb myfile2.rb to try out things and then able able to “go back”. Rock and roll.

I’ve been hankering to try out programming for a device. I did a hello world app for my pocketpc with c++ years ago. Apparently it didn’t hold my interest cuz I never did much with it. I am not enthusiastic about the iphone and its developer requirements and restrictions (way to piss on the developers Apple). So I’ve been wanting to try out Android for awhile now.

I was excited to see a new beta book out by Pragmatic Programmers — on Android! hot dog! I bought it the same day and installed the SDK, Eclipse and the Eclipse plugins on my macbook. I installed an emulator too. It worked without a hitch — which I kinda expected there to be some goofy java error or some nonsense that would frustrate and discourage me. Nope — worked real easy.

The intro was real easy to understand and explained the different components. The book tells you about a soduko game that you will develop over the course of the book. I like that idea and I think its an excellent way to learn.

I do the Hello World application following the instructions in the book (yeah I know there are hello world tutorials on the google site). Whoo hoo! It worked! Excited I move on.

The next example I had some problems with. I think some code is missing from the book. I looked high and low and emailed the author and logged a issue at PragProg.com … no response yet, but I figured out if I just removed that part of the code then it worked. Just didn’t have a background color set. Oh well — I’ll be patient to see if someone gets back to me or its fixed in the next version of the book.

Moving on, I see code snippits that should be in certain files but not sure where in the file they go and I think sometime they are missing some import statements. I get errors and I poke around to figure it out ..and I give up for the moment … :(

Its a beta book.. so.. guess we’ll have to wait till the next version comes out..

Meanwhile I checked out some of the examples that Google has on their site.

The author got back to me and told me to check out the source code for the sample program….and he’d try to make it more clear about the background color.

I will keep plugging on!

Alright, ok! I am setting my ruby and rails down for a bit to give python and django a spin. I’ve been bored with programming lately and need to try something new. So what the heck! Lets see how this goes.

Since I am not a guy — I read the instructions first! (haha). I open the README file and it says to read install.txt then proceed to the tutorials in the docs section. Sweet! Being as I an off-the-grid commuter, I have to rememember to download all documentation BEFORE I leave the house in the morning.

First thing I had to do was install Django. I already had Python 2.5.1 installed on my mac. You have to be connected to the net to install Django, unless you already have the python utility “setuptools”. Oh crap! Hopefully I have it! I did a search and found it was already installed. Whew! I untar the Django tarball and type “python setup.py install” … rock and roll!

Now, proceeding in orderly fashion to tutorial01.txt I see the command to create a django site:

django_admin.py startproject samplesite

BURN!!!!

-bash: django_admin.py: command not found

HUH! :( … ok.. so maybe django didn’t install right. I do a search and I found it installed in Python/2.5/site-packages/django/bin .. oh, doh! I used an _ (underscore) instead of a - (dash).

*smack forehead* … as often happens with a new thing, I get way to excited and forget the small details. The devil is in the details!

Lets try again:

django-admin.py startproject samplesite

HEY! It works, imagine that. Using the right command works. :) yay

Hmmm.. contrast the starting struture of an new Django site with Rails:

Django:

-rw-r–r– 1 nstowe 499 0 Apr 14 08:10 __init__.py
-rw-r–r– 1 nstowe 499 153 Apr 14 08:12 __init__.pyc
-rw-r–r– 1 nstowe 499 546 Apr 14 08:10 manage.py
-rw-r–r– 1 nstowe 499 2812 Apr 14 08:10 settings.py
-rw-r–r– 1 nstowe 499 1763 Apr 14 08:12 settings.pyc
-rw-r–r– 1 nstowe 499 233 Apr 14 08:10 urls.py
-rw-r–r– 1 nstowe 499 253 Apr 14 08:13 urls.pyc

Rails:

Interesting… Moving on! Next part of the tutorial is starting the server:

python manage.py runserver

Yay, the server starts right up.

I edit the settings.py file and tell my site to use sqlite3 and dbfile of samplesite_development.db (wondering, does django use different dbs like rails? dunno, but I am used to this convention now!)

The last section of the settings file has a list of installed apps (seems like the Rails equivalent of a plugin):

INSTALLED_APPS = (
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.sites’,
)

According to the tutorials these are included for convenience and each of them have at least one table. So we need to create the tables –

python manage.py syncdb

This creates all the missing tables for anything in the INSTALLED_APPS. When I did it, it asked for an admin name, password and email. Thats pretty cool, I don’t recall any interactive propmpts for any rails plugins

Quote from the docs:
” Django comes with a
utility that automatically generates the basic directory structure of an app,
so you can focus on writing code rather than creating directories.”

Great, I hate creating directories. Actually, I believe one of the benefits of having a tool create a basic site structure is consistancy. Another Django developer can walk in and go “oh yeah, I know where stuff is.”

As I read the tutorial01.txt I realize that i just created a django project — not an app. Now to create my app, a voting application.

It creates a directory containing the following:

-rw-r–r– 1 nstowe 499 0 Apr 14 17:05 __init__.py
-rw-r–r– 1 nstowe 499 57 Apr 14 17:05 models.py
-rw-r–r– 1 nstowe 499 26 Apr 14 17:05 views.py

Interesting, a different concept then rails. In Rails when you do

rails my_awesome_site

It create a webapp structure or one site. Not very easy to combine two Rails apps into one.. In fact, as I know it, damn near impossible. Best way to do that is subdomains as I see it.

Moving right along.

Time to create model files.

To define a model for a pol in Django:

class Poll(models.Model):
question = models.CharField(maxlength=200)
pub_date = models.DateTimeField(’date published’)

A migration file in Rails

create table as |t|
t.column :question, :string
t.column :pub_date, :datetimeo
end

and then the model file simply is:

class Poll
end

Rails, as you probably know, uses instrospection to figure out the field names…contrary to the approach that Django takes.

One of the differences is that with this one file, Django has the creation of teh table AND the model defination, where was Rails has two files. Interesting.

Ah, here we go. Since we created this application Polls we need to “add” it to our projects, you know where we saw those default “plugins” I called them.

INSTALLED_APPS = (
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.sites’,
’samplesite.polls’,
)

There is our poll app.

Now we need to generate the sql:

python manage.py sql poll

Thats the command that shows you what the SQL will look like, you need to run this command to actually execute that code — sync up the db!

python settings.py syncdb

In Rails, to run a database migration you would do:

rake db:migrate

In Rails all the database migration have a version number, making it easy for you to go “up” a version and “down” a version. This is a nice concept, but its a nightmare with a team more than 2 people! So this is no longer a novel concept to me. I think I’d rather let subversion do my version control.

I am writing a rails application for my mom to enter her bakery orders for her business. The Order page has a form to search or add a new customer, so i can streamline the process as much as possible. In the Order create method, I either get a customer array or a customer ID.

Here's the order create method in the controller:

RUBY:
  1. def create
  2.    
  3.     new_customer = params[:order][:customer_id].blank? ? true : false
  4.    
  5.     if new_customer
  6.       @customer = Customer.create(params[:customer])
  7.       if @customer.valid?
  8.         @customer.orders.create(params[:order])       
  9.         flash[:notice] = 'New customer was added and order was created successfully'
  10.         redirect_to orders_url
  11.       else
  12.         flash[:notice] = 'Please fill in all required fields'
  13.         render :action => 'new'
  14.       end
  15.     else
  16.       @customer = Customer.find params[:order][:customer_id]
  17.       @customer.orders.create(params[:order])
  18.       flash[:notice] = 'Order was successfully created.'
  19.       redirect_to orders_url
  20.     end
  21.  
  22.  end

Ok? anybody see a better way to do that? It took me awhile to figure out the right branching. Now for my rspec tests. This can take two paths -- new customer or existing customer.

Test for New Customer:

RUBY:
  1. describe OrdersController, "handling POST /orders with a new customer" do
  2.  
  3.   before do
  4.     @order = mock_model(Order, :to_param => "1")
  5.    
  6.     Order.stub!(:create).and_return(@order)
  7.    
  8.     @params = {:customer_id => nil}
  9.    
  10.     @customer = mock_model(Customer, :to_param => "1")
  11.     @customer.stub!(:orders).and_return(Order)
  12.     Customer.stub!(:create).and_return(@customer)
  13.   end
  14.  
  15.   def post_with_successful_save
  16.     @customer.should_receive(:valid?).and_return(true)
  17.     post :create, :order => @params
  18.   end
  19.  
  20.   def post_with_failed_save
  21.     @customer.should_receive(:valid?).and_return(false)
  22.     post :create, :order => @params
  23.   end
  24.  
  25.   it "should create a new customer" do
  26.     Customer.should_receive(:create).with(anything()).and_return(@customer)   
  27.     post_with_successful_save
  28.   end
  29.  
  30.   it "should create a new order" do
  31.     Order.should_receive(:create).with(anything()).and_return(@order) 
  32.     post_with_successful_save
  33.   end
  34.  
  35.   it "should redirect to the new order on successful save" do
  36.     post_with_successful_save
  37.     response.should redirect_to(orders_url)
  38.   end
  39.  
  40.   it "should re-render 'new' on failed save" do
  41.     post_with_failed_save
  42.     response.should render_template('new')
  43.   end
  44. end

Hows that look? do I catch all the cases for the New Customer scenario?

Lately I've been burned out on all things programming, even ruby. I need a break from the usual. I've been playing World of Warcraft. It appears as usual, i am late to the game. Everyone I talk to is "oh yeah! I used to play that" ... I was hoping I could find some of my geek friends in the game, oh well!! I've found some friendly people and have made a few friends.

Its quite a fun game. I find the professions very fun. I guess thats why I have 6 characters so far... I started a wow blog so I could write about them! The url is wow.rubygeek.com :) I used a default template, since most people use a rss reader I don't see much point in making a pretty site!

misc stuff:

DevChix is growing like crazy! We have well over 100 members.

Its finally getting warmer out ... this will help my winter blues I'm sure. I've been feeling more motivated to get my lazy behind to the gym too. yay!

I upgraded one of my sites to Rails 2.0 .. didn't have any problems, just needed to upgrade rspec to latest too, so not too bad. I was using a dev version of rails from last fall, but I still expected there to be a few bumps in the road.

Getting Things Done - we had a seminar on this at work and it re-inspired me to get stuff done, in fact, thats the only reason I have this post out now :) Its on my list! :)

I'm in the middle of a 5 day vacation ... so I'm going to get back to chilling out :)

Published by Apress
By Kevin Marshall, Chad Pytel, Jon Yurek
Book Info
Sample Chapter: Ch. 01 - Introducing Active Record
Table of Contents

Years ago when I was in PHP Land (now I travel quite a bit more! haha), I strugged for months with how to write a good ORM . It was tough, because I wanted to abstract the "boring logic" of retrieving records from a database without writing SQL but still remain flexible enough. I never really came up with a good model. I used the DAO from "extreme php" library which I think was a knock off from java. It was ok, but I still didn't feel like I had "arrived".

When I discovered Ruby on Rails, I found ActiveRecord. Ahh HA! Finally, this is what I was looking for. At first I thought it was part of Rails, but its not. Its a standalone library and you can use it with straight up ruby scripts.

I got a review copy of "Pro Active Record" some time ago and read it some when I got it, then some later, and now I am going to officially write up a review!

If you do anything with Active Record, get this book. The things that are briefly mentioned in most Rails books are described in detail in this book.

Chapter 1 - Introducing Active Record

Most of the time, the first chapters of a book are boring to me. I don't need another "History of the Internet" or how "HTML was developed" ... blah blah. But this one, the story is only 1 page. And it actually has some introductory scripts on using Active Record, so you can see right away how it works. It also explains the benefits of MVC and why ORMs are good. Some people still don't get it!

Chapter 2 - Active Record and SQL

This chapter helps you translate the "sql in your head" to how to write it with Active Record. I've used Active Record so much that now I have forgotten most of my SQL, which is kind of embarrassing. :) I now find writing sql tedious and boring! I would have actually called this chapter "Demystifying Active Record" since it explains why all the dynamic finders work. You'll also find transactions and locking explained here.

Chapter 3 - Setting up Your Database

Migrations! The Awesome Thing that can turn into a nightmare for large rails projects with multiple developers.... definitely have to decide on some best practices with your team on this one. The chapter has only one thing to say about this -- assume any checked in migration has already been run by your team and the migration should not be edited and checked back in! You'll have to make another migration file with your changes.

[tip]
Nola's Note: When you make a migration, test it both UP and DOWN!! Here's what I do --
write a migratiion
rake db:migrate (go up to the version with new code)
rake db:migrate VERSION=n-1, (go to version before the latest)
rake db:migrate (back to lastest)
rake db:migrate VERSION=0 (back to blank db)
rake db:migrate (back to latest)
[/tip]

Just to be sure its all good -- even on a new database!

Chapter 4 - Core Features of Active Record

Now is the fun stuff - Callbacks. This is magic. This makes Active Record so flexible, and is one thing I could never figure out how to do with my PHP ORMs. I use call backs to set defaults for fields. If its just a straight default, then I set it in the database but if I need to make a decision, (if this field then this field..) then I can use it in a callback.

Associations - at first this is very confusing! I don't know how many times I got "has_many" and "belongs_to" mixed around in the beginning.

Validations - Awesome. I had to do some ruby code without a database and I really really really missed the validations. It took me like 5x longer than it should! Understanding all of these validation methods will make your life so much more enjoyable. I really really hate doing boring, repetitive stuff...it seems so wasteful to me.

Chapter 5 - Bonus Features

Everybody likes a bonus and this isn't even the last chapter of the book.

Java people will like the Active Record Observers -- seems a little AOP to me (aspect orienteted programming) and something I probably have neglected to use to their fullest extent.

Acting up -- Learn how to "save time" with the "acts_as" magic: List, Tree, Nested Sets. If your data needs these structures, you got it made. I can imaging how much longer it would take to write this stuff in perl or php.

Composed of - I haven't used this, but this looks like a good way to make sensible objects out of database tables. There is quite a bit of explanation and examples of this, it will come in handy.

There are a few other in depth explanations of things, such as method_missing which is how alot of the magic happens. Rock on.

Chapter 6 - Active Record Testing and Debugging

Ahh yes, Testing. My favorite subject. My friends who know how much I love testing say I am sick. I must have an inner need to PROVE I am right or something, haha.

The chapter goes into depth about using test_unit with Active Record, sadly no RSpec. But, it does go into all the error messages that Active Record throws so you can write good try/catch blocks and make very exact error messages (probably best logged for the admin rather then displayed to the user!)

Chapter 7 - Working with Legacy Schema

Here's how you work with that old database that just won't die... or that management won't let you totally redo. Active Record follows some of the principles of Rails "convention over configuration" ... relying on table and column naming conventions to figure out how to build your object....but still giving you a way out if you want your tables singular and your primary id field called "myawesomeid" instead of "id"

I've used some of these things on an older database and it was possible! Not too bad if thats what you have to work with.

[soapbox]
Some people find this annoying "oh gosh! my library can't make decisions for me! OMG! That sucks" .. to that I say, "Umm ok. But if you follow these conventions then I can come into your project and know exactly what is going on" ... like with web standards, we all harp on how IE and FF do things differently, yet people want to bellyache about Active Record preferring to have plural names and id field called "id". Right.

Follow the dang convention and find something worth complaining about to complain about. :)
[/soapbox]


Chapter 8 - Active Record and The Real World

This chapter goes into depth about the library and encourages you to go read the Active Record code. Always a good idea to know what it is you are using :) I've actually learned ruby better by reading source code. The chapter walks you through basic structure of the files. Very cool.

[soapbox]
I used to work at a place that didn't like any "outside code" because they were afraid "OMG ... it will send our passwords to Russia!" ... ok, well I am not an idiot. I read over any code that I use that I didn't write. I look at the tests to see if I am using it right. I even RUN the tests so I can be sure its working as advertised.
[/soapbox]

Alternatives to Active Record - with EXAMPLES! If something about Active Record doesn't set too well with you, take a look at the alternatives. Sometimes I look at the alternatives and decide that the first wasn't so bad after all. You'll find examples of DBI, Og, ActiveRelation.

Finally a section on Q and A finishes up this book. The Appendix has a complete reference of ActiveRecord methods to make this book a well rounded reference, tips, documentation and very handy to have at your desk!

Next Page »