Rails’ symbols: don’t create them dynamically!

February 20, 2009

No Gravatar

Symbols are great, we use them in hashes all the time…but did you know, symbols aren’t part of Rails’ garbage collection? When you create them, the memory the use has been allocated permanently! Why is this bad? So if we create a handful of symobls…no biggie, but what if we’re dynamically creating lots and lots of them? Major faux pas!

This was brought to my attention last night when I was trying to create a hash with some dynamic symbols. See below:

 
## here is some data
 
>> hardware = Hardware.find(:all, :limit => 2)
=> [#<Hardware id: 1, name: "Bolts", description: "Lorem ipsum dolor sit amet, consectetur adipisicing...", photo_front_file_name: "144_replica_wheels_Closeout_30_percent_off_prices_l...", photo_front_content_type: "image/jpeg", photo_front_file_size: 80842, photo_angle_file_name: nil, photo_angle_content_type: nil, photo_angle_file_size: nil, created_at: "2009-02-19 11:04:48", updated_at: "2009-02-20 20:29:46">, #<Hardware id: 2, name: "Nuts", description: "Lorem ipsum dolor sit amet, consectetur adipisicing...", photo_front_file_name: nil, photo_front_content_type: nil, photo_front_file_size: nil, photo_angle_file_name: nil, photo_angle_content_type: nil, photo_angle_file_size: nil, created_at: "2009-02-19 11:04:48", updated_at: "2009-02-20 07:18:32">]
>> y hardware.first
--- !ruby/object:Hardware
attributes:
  name: Bolts
  updated_at: 2009-02-20 20:29:46
  photo_angle_file_size:
  photo_front_file_name: 144_replica_wheels_Closeout_30_percent_off_prices_listed_.jpg
  photo_front_file_size: "80842"
  id: "1"
  description: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  photo_angle_content_type:
  photo_front_content_type: image/jpeg
  created_at: 2009-02-19 11:04:48
  photo_angle_file_name:
attributes_cache: {}
 
=> nil
 
## don't do this
for hardware_item in hardware
 formatted[:#{hardware_item.name.downcase}] = hardware_item.description
end
 
>> formatted[:bolts]
=> "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
 
## this is better
for hardware_item in hardware
 formatted[hardware_item.name.downcase] = hardware_item.description
end
 
>> formatted['bolts']
=> "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

{ Comments }

rake db:migrate for production databases

February 19, 2009

No Gravatar

After I updated one of my applications that I’ve been working on, I kept running into an internal server error. I looked at the logs and for some reason none of my SQL queries were working.

Looks like I ran “rake db:migrate” and migrated the development database…not the production one. I scratched my head trying to remember the command. Here it is!”

rake db:migrate RAILS_ENV=”production”

{ Comments }

All Rails crash course tutorials are available on GitHub!

February 18, 2009

No Gravatar

ZIP files are silly :)

I’ve created a GitHub repository for all the tutorials to date and plan on using GitHub to version control all future applications. I will be editing the posts and removing any old download links shortly.

The repository is located at:
http://github.com/ng/crashcourse/tree/master

Want to check out the code? Run this in terminal:

git clone git://github.com/ng/crashcourse.git

Don’t have git installed? If you’re on OSX it’s simple!
http://code.google.com/p/git-osx-installer/

{ Comments }

Paperclip doesn’t accept multiple files in Rails 2.3

February 17, 2009

No Gravatar

Those of you who have upgraded to Rails 2.3 may have encountered a bug where Thoughtbot’s Paperclip doesn’t want to accept multiple file attachments. I checked my models, migrations, and forms but I couldn’t figure it out for the life of me.

It turns out there is some kind of bug with the Rack interface that is wonky in 2.3. By updating to edge Rails I was able to fix this little bug.

{ Comments }

MySQL gem on OSX Leopard 10.5.x

February 17, 2009

No Gravatar

sudo install_name_tool -change /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib /Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle

I started to play with Rails 2.3 and ran into a little issue trying to access my database. I guess I shouldn’t have been ignoring all those depreciation warnings…oops. Naturally, the first thing I did was run “sudo gem install mysql” which threw the following error:

ng$ sudo gem install mysql
Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
	ERROR: Failed to build gem native extension.
 
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb install mysql
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
 
Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/mysql-2.7/gem_make.out

After digging around, I found the following command:

sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
Building native extensions.  This could take a while...
Successfully installed mysql-2.7
1 gem installed

Looks like it was successful…but wait, let’s check. Pop into IRB:

>> require 'mysql'
LoadError: dlopen(/Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle, 9): Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib
  Referenced from: /Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle
  Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle
	from /Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle
	from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require'
	from (irb):1

Further searching revelaed the solution:

sudo install_name_tool -change /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib /Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle

{ Comments }

adapter: mysql (ArgumentError)

February 16, 2009

No Gravatar

I was up late working on a project and I kept running into this error after I tried to deploy to my staging server. It turns out you can’t use tabs in a YAML file. Oops..after I removed the tabs and used spaces my app started successfully.

 
/usr/lib64/ruby/1.8/yaml.rb:133:in `load': syntax error on line 18, col 3: `   adapter: mysql' (ArgumentError)
	from /usr/lib64/ruby/1.8/yaml.rb:133:in `load'
	from /usr/lib64/ruby/gems/1.8/gems/rails-2.1.2/lib/initializer.rb:716:in `database_configuration'
	from /usr/lib64/ruby/gems/1.8/gems/rails-2.1.2/lib/initializer.rb:340:in `initialize_database'
	from /usr/lib64/ruby/gems/1.8/gems/rails-2.1.2/lib/initializer.rb:124:in `process'
	from /usr/lib64/ruby/gems/1.8/gems/rails-2.1.2/lib/initializer.rb:97:in `send'
	from /usr/lib64/ruby/gems/1.8/gems/rails-2.1.2/lib/initializer.rb:97:in `run'

{ Comments }

unable to read font during ImageMagick/RMagick installation

January 15, 2009

No Gravatar

I was installing Rmagick earlier on OSX and ran into a little hiccup, during the installation I recieved the following error:

unable to read font `/System/Library/Fonts/Times.dfont’

After about 30 minutes of digging, I found the solution! Open up Font Book and remove any duplicate fonts; duplicate fonts have a small black dot next to their names.

{ Comments }

People Search on Google

October 8, 2008

No Gravatar

Why doesn’t Google allow advertisers to buy “Proper Names”? Proper name searches are one of the most popular searches that people do on search engines right? What are five top things that you do on the internet nowadays?

1. Check your email
2. “Google” someone
3. Read news on how McCain is losing in the polls
4. Check your flailing stocks
5. Facebook (has that become a verb yet?)

That is just my list but is probably true to a certain extent. I think I must Google someone at least once a day and people are always looking for lost friends, loved ones, and classmates. Google a while back made a decision to scrub all paid sponsored listings for any “Proper Name” searches. Why would you turn your back on all of the revenue that could be made from those searches? There must be millions of searches a day on people’s names that Google could be monetizing and they use the excuse that users complained that there were paid ads on their names and that Google was invading their privacy. Couldn’t they just create rules around advertising on “Proper Names” so that they weren’t deceptive or confusing as to what the product offering was?

Just imagine how many advertisers would probably like to advertise for Proper Names such as public record companies, White Pages companies, Reunion, Classmates.com, Friends Reunited, Ancestry.com, Genealogy.com, LinkedIn.com, ZoomInfo.com, eBay, Amazon, etc.

Just do a search on Google for “Neil Clark Warren“, shouldn’t there be ads for books on Dr. Warren, links to eHarmony.com, etc. Neil Clark Warren is the founder of a very popular online dating service, eHarmony.com. His name alone probably gets searched 1,000’s of times a month for people doing research on his Relationship Compatibility Test.

Now if you do that same search on Yahoo you will see a full marketplace of ads on Neil Clark Warren and guess who is buying an ad? Yup, Amazon.com and a couple other book stores since he is a published author. Is this finally making any sense to you? The only sense I can gather from it is that Google thinks that they should be able to get you all of the information you need on people in their natural search results even though most of the public record information isn’t crawlable content on most websites.

For full disclosure on the topic, I used to work for a public records company that spent a good deal of money on public name searches and when Google made the decision to remove all proper name searches they weren’t too happy. I currently don’t have a client that buys proper names but still to this day can’t understand why they are still not allowing advertisers to buy generic names. Instead, we have to read IMDB and Wikipedia listings on people if they are important enough to have a page created on their name. Come on Google, get with the program dude.

-Brian R.

{ Comments }

SEO Best Practices – Frequently Asked Questions

October 6, 2008

No Gravatar

Here are some answers to questions clients often ask me.

Q: I want to be number 1! How do I do it?
A: This always comes up. When it does I proceed to ask, “What do you want to be #1 for?” Time and time again the responses I get back range from “I’m not sure” to some type of single keyword.

Hey guys, it’s 2008! Search has evolved from single keyword searches to phrases and tail terms. Instead ofsearching for “thai food” you’ll find people typing in “seattle thai.”

By targeting these specific phrases, the chances that you can own the top search listings increases substantially.

Q: Keywords, OK. Where do I start?
A: Figuring out which keywords to target take time. When we’re blueprinting a SEO campaign, we start by determining what visitors might be searching for. Here are some of the tools we use.

Google Suggest
Many browsers support “autocompleting”, this features offers search query suggestions to users. For example, as I type “seattle thai” in Firefox, Firefox begins guess what I mightbe looking for. If you know what’s going to be suggested, you can better optimize your site.

Google Trends
Trends provides temporal data on keyword search data. If you’re trying to determine which keyword phrases you want to target, but are unsure of the direction to head GoogleTrends can show you the average search volume and whether it’s becoming more or less popular.

Google AdWords
Believe it or not, AdWords is useful for more than just paid search. We use AdWords to get a rough estimate on keyword search volume and keyword competition. AdWords accounts are free, signup.

WordTracker
Everybody searches for different keywords. Do you have your keywords covered? WordTracker allows you to trackdown related keywords. If your visitors are searching for “hawks tickets” but you’re targeting “seahawks tickets”, will they find you? WordTracker also offers a free keyword research guide.

Q: Should I submit my site to web “directories”?
A: It won’t hurt, but don’t waste your time. Anybody who suggests that you submit your site to directories like Yahoo or the DMOZ (a dated, open directory project) have no idea what they’re talking about. On a related note…stay away from “link exchanges” or “web rings.” These will cause you nothing but headaches.

Q: What are some quick and easy SEO methods?
A: I like lists. Here’s some low-hanging fruit, in list format.

1. Make sure your websites XHTMLand CSS validate. Start with the XHTML, you can do it free at: http://validator.w3.org/. Why? Even though your webpages show up correctly, broken code can confuse search engine spiders. If a spider can’t crawl your site, it’ll give up, and move on.

2. Use your keywords in your bodycopy. If you’re targeting “seattle thai” but your body copy is completely unreleated…guess what’s going to happen?

3. Add a footer and link to your homepage with your primary keyword. For example, my blog’s URL is http://www.jonathansng.com; if you look on all of my pages, I link to it as “ruby on rails blog.”

4. Use keywords in your title tag. Again, using my blog as an example I set my title to “jonthan ng – ruby onrails blog.”

It’s 1:00 AM, I should probably get to bed, so that’s it for now folks. I’ll be hosting an event shortly, if this was beneficial to you, be sure to drop in!

{ Comments }

Google Has Great Search Results – Not

October 1, 2008

No Gravatar

This is my week to rant about Google so just let me be for a second. I have been doing random searches everyday in areas and verticals that I don’t typically search on just to find some funny search results that don’t make sense or where the SEO pro’s have worked the “Google Algorithm” also referred to my me as “Google Love’s How Popular I Am” tool. We already know Google doesn’t care about your inbound links or your PR link backs or your PageRank score. They get their data from Analytics, Feedburner, links from trusted sources which are usually news publications (Authority links), Registrar info on the age of your site, how active the content is and the list goes on. So what happens when all of your competitors are employing the same tactics to Google? Check the “Nordstrom Coupons” search example below:

Isn’t this reminiscent of how Altavista.com search results used to look back in 1999 before they went under?

Now let’s look at how the search results used to look back in January, 2001 now that Google has published their own ‘way back index machine’ for us to view the Google 2001 results for Nordstrom Coupons:

2001 Nordstrom Coupons search

What has changed in 10 years? Looks like the consumer is getting a pretty shitty experience and where are the ads? Oh, Google doesn’t think that advertisers should be able to buy this keyword I guess because it isn’t relevant even though tons of those same advertisers are now SEOing the hell out of it. This results are starting to look a little homogeneous.

I wish our friends at Mahalo.com would hurry up and get all of their index filled up and start spending some money to get some mind share in search.

Mahalo.com Nordstrom Coupon search

{ Comments }