giggityHere’s a fun snippet of code we wrote today at AppAcademy.

Basically the goal was to take a bunch of couples who were into swinging and swap all of their partners.  Yep, swinging.. as in:

Swinging or (rarely) partner swapping is a non-monogamous behavior, in which singles or partners in a committed relationship engage in sexual activities with others as a recreational or social activity.[1] Swinging can take place in a number of contexts, ranging from spontaneous sexual activity at informal gatherings of friends to planned regular social meetings to hooking up with like-minded people at a swingers’ club. It can also involve Internet-based swinger social networking services online.
Wikipedia

In order to properly swing, a couple is supposed to show up at a party and swap their partner for another partner.  For our purposes we were to assume couples were heterosexual males and females.

The code we came up with was this:

Basically we found that we could randomize all of the couples then shift their partners by one.  By doing it this way we could ensure things that both things were spicy AND that a man never ended up with the same woman he arrived with.

Ned Ruggeri - AppAcademy teacher

Today marks the first day of my sprint through San Francisco based AppAcademy. It’s a pretty interesting setup.  Basically, you’re paired with a random person everyday and work through a large suite of problems assigned for that particular day.

To say the least, you and your pair bust ass HARD until the end of the day.  The day ends with more work — you are now assigned another pair and must review their code.  After reviewing the other pair’s code and sending them some comments the final part of the day (now night) is doing the readings for the next day’s schedule.

It’s very intense but the amount of code we are producing is really fantastic.

The teachers and TAs are VERY approachable.  Ned Ruggeri (mug shot on the right) presents complex concepts in great detail and definitely seems to genuinely care if people are “getting” things or not.

It’s a great environment.

I recently setup my development system from scratch and one of the tasks is installing PostgreSQL as my apps are almost always deployed to Heroku and they <3 PG.

As you may or may not know, Lion ships with a castrated Postgres installation which (if you don’t already know this (!!!!)) can make it challenging to use something like Homebrew for your installation medium.

To avoid this whole situation I suggest you checkout Postgres.app.  It’s written by the guys @ Heroku and is essentially a .app that you drop in your Applications folder.  Run whenever you want a PG server — it’s THAT easy.

Well… Sorta…

After installing it I had some issues but was able to figure it out so that’s really the “gotcha” part of this post.

On first launch i realized i couldn’t connect to the PG server and immediately went to my system’s console for clues.  This was sitting there looking pretty:

 

Ye olde  “Failed system call was shmget” error….

I believe the root of this issue is that OSX’s shared memory configuration is just really small. And after Googling around I found this post on Railscasts.com by Denis Savitsky (aka sadfuzzy).

In a nut shell there are two ways to fix this:

1- Temporary Solution: Gone after reboot.

2- Permanent Solution: Forever, forever, ever, forever, ever? Yep. After reboot the settings will stick.

 

You can also see this post for further details.

 

Some Preface

An app I’m working on has a rake that that basically gathers data from the web.  Doing things serially is really REALLY slow and to get around this I started using the (fantastic) parallel gem by Michael Grosser.

This gem is great — it’s simple to use and in something like 3 minutes after installing it I had my task running in 10 processes and absolutely murdering the work that needed to be done.

The Issue

I swapped databases not too long ago and moved from MySQL to PostgreSQL for full text search (which i don’t use anymore) and to stay in line with my host, Heroku.

Today i attempted to run the task against my PostgreSQL db and came up with an error I had never seen before:

message type 0x5a arrived from server while idle

WHUT?

After some investigating on Google I found the root cause — PostgreSQL does not allow using the same connection for more than one thread.

That’s pretty straight forward.

I believe the issue was that I had something like 10 ruby processes that were spawned from 1 process that was holding the db connection.  Not allowed!

The Solution

The solution is actually very straight forward as well.  To get around this you simply need to reconnect to the database each time you spawn a process or thread.

What does this look like in code?

Before (bad) :

After (GOOD):

 

And that’s pretty much it.

A Little Back Story…

Migrating from MySQL to PostgreSQL is not as straight forward as it sounds.  As you may or may not know, MySQL’s ‘mysqldump’ utility comes with a “compatible” option where you can specific “postgresql” but the catch is this:  the resulting SQL is not properly escaped and will need it’s data types augmented.

In other words… or in my words, the SQL generated from mysqldump that’s meant to be compatible with postgresql is basically worthless in it’s exported state.

 

How I Migrated

Thanks to people smarter than me, (specifically Max Lapshin) the process of dealing with the above is pretty straight forward with the help of mysql2postgres!

Here is what I did to migrate my MySQL data —  a few million rows clocking in just under 1 gig.

 

Step 1:
Install the mysql2postgres gem.

Before you just go and clone the gem to your local system, take a look @ the forks as they might have something interesting you want.  At the time of this writing, maxlapshin’s repo had like 5 or 6 outstanding pull requests.  My example code will just use his repo as the clone source.

 

Step 2:
Configuration

After installation, the ‘mysql2psql’ command should be available.  If it’s not, just close your terminal, open a new one and navigate back to where you cloned the gem.

By default you won’t have a configuration file.  Run the ‘mysql2psql’ command and it will generate a default one for you.  Once you do that, just open it up and configure it to your heart’s desires.

 

Once you open the configuration file, just read it top to bottom.  Everything is commented and it’s very straight forward stuff.  Fill in your MySQL details, then fill in your postgres details (or use the file: option).

Last part is to run the migration.

Step 3:
Run the Migration

This part will *probably* be as simple as doing this:

 

If that worked for you then enjoy the sweet sweet glory of being migrated.  Go check your data, utf8 encodings and all that fun stuff.

If that did not work out and you got an error like the following… then you might be using a mac, along with homebrew and can easily fix it!

Le Error

The Fix (aka How to Install mysql-connector-c on a mac)

  1. FIRST get rid of homebrew’s mysql-connector-c with: ‘brew remove mysql-connector-c’
  2. Go to MySQL’s download page and grab the Connector/C for your flavor of OSX
  3. Extract it and do the following:
    1. Copy the lib/ files to /usr/local/lib
    2. Copy the include/ files to /usr/local/include
    3. Copy the bin/ files to /usr/local/bin
  4. Profit.

Note: I’m still not fully sure why this fixed it for me.  The version i install from MySQL is the same version Homebrew uses. :/

 

To start off, the friendly_id gem allows your models to have “slugs” which ultimately help when you want your urls to appear more descriptive. For example instead of site.com/cars/43  you will get something like: site.com/cars/slug-name .

It’s better for SEO and IMO provides a better experience for situations where someone copies a link and sends it to another individual.  When you see a descriptive URL you kind of know what to expect and it feels less threatening than site.com/?blah=92349&whatever=5959 ….. OR i’m just a wussy.

My current situation is this:
I have some STI models that use inheritance and want to use friendly_id with it.

The problem i ran into and erroneously thought was a bug can be found here:
https://github.com/norman/friendly_id/issues/167 .  Basically when creating records for inheriting models i was getting duplicate slug errors.  Something like: “Error: Duplicate entry blah blah”.

A quick answer the gem’s author, Norman, rained light and joy on where i had gone wrong.

The Solution:

So how do you get STI models and friendly_id to work together?  This gist says it all and uses the idea of cars/brands as the STI model.  Note: this is really only for example, if you have a lot of cars and brands you will probably want to model that differently.

Read the docs:

The gem’s docs are really good.  While the docs for the FriendlyId::Scoped module explain everything in concise terms here they do not explicitly cover STI so hopefully this helps you out if you’re in the same situation.

My use case for this is that i have a rails app using STI.  I’ve got an array of results from all of my inheriting models and i’d like to sort them ALL based on a common attribute they share — a zip code.

Here is a RIDICULOUSLY simple way to use Ruby to sort the entire array — please note though, the code is querying for “Model.all” just to make the example simple — in my REAL code i have more complicated search criteria.

 

I’m using Twitter’s Bootstrap CSS framework and it’s pretty nice.  One of the cool bundled features of the framework is that they provide classes for displaying what rails hackers know as Flash messages.  The problem here is that Twitter has named them different from what Rails people traditionally use so you cannot just drop in Bootstrap and expect your flash messages to appear properly.

I did NOT want to modify the original bootstrap css and tack on extra classes so I got around this with an application helper method that does some translating for me.

Here is the code: