Active Record Is Not Null In Where Clause

I had a difficult time finding out how to do this the way I wanted. Let's say you want to do a check for a value not being null along with other logic, say an IN clause. You're led down a path of forumulating a string that gets hairy ids = [1,2,3] SomeRecord.where("some_column is not null and id in (#{ids.join(','})") That's kind of gross. I've done it, and I've seen it done. This is just a trivial example, if you have more conditions you can be tempted to expand that string. However, there is a cleaner way. ...

August 2, 2012 · 1 min · 113 words · Brandon Harris

Rails 2 to Rails 3.1 : A Road to Hell

Some say that the road to hell is paved with good intentions. For a recent project for a client, we decided to upgrade their aging app from 2.3.14 to 3.1.3. Why? A lot of the plugins and gems that we were using were getting stale, and it was getting harder and harder to support the aging setup. I set out my journey with the fantastic rails_upgrade plugin. I had done several upgrades in the past, and I knew a lot of the pitfalls, but I wasn't quite prepared for what I was about to discover. ...

February 8, 2012 · 5 min · 905 words · Brandon Harris

Rails Simple Page Caching

Memcache is great, and Redis is even better. But you are still bound to a database, and running services. We are so caught up in making everything dynamic, that I think we don't often realize that we are still serving up mostly static html. The good news is that Rails has static html caching built in, and I rarely notice anyone talking about it. The scenario: You want to input your page content via an administrative interface, but this data rarely changes. Another way to look at it, the content only changes if you or someone who administers the site has a need to change it. Rails page caching is your friend. ...

April 12, 2011 · 3 min · 501 words · Brandon Harris

JSON Templates in Rails

Sometimes, you just don't want to do call the "to_json" message on an object. It could be that the object has a lot of attributes, the json needs are very simple, or you simply don't like the default structure. Fortunately, there is a very easy solution: JSON templates. This is built in, and you might have not even known it. In my case, I wanted to plot a bunch of people and their locations on a map. I had the following model: ...

March 29, 2011 · 2 min · 243 words · Brandon Harris

Painless Upgrade to Bundler and Bamboo Stack

I just upgraded this site to use bundler and the heroku Bamboo stack. The steps were: 1. Remove the .gems file, and create a Gemfile. 2. Patch the Rails 2.3 installation for bundler 3. Run heroku command: heroku stack:migrate bamboo-ree-1.8.7 4.Run command: bundle install 5. Commit all changes to git, and push to heroku. I am really impressed with the ease at which this was done. Heroku continues to impress, and Bundler is the best thing to happen to Ruby since Rails. ...

November 6, 2010 · 1 min · 82 words · Brandon Harris

Oracle, Rails and Ubuntu 10.04

I recently setup an Ubuntu based server that needed ruby to talk to oracle and mysql. If you are a rails developer, you should be familiar with mysql, but what about Oracle? It isn’t quite as straightforward as you might assume, but it is within the grasp of mere mortals. Please keep in mind that due to changing version numbers, all version numbers are replaced with *. It is up to the reader to properly translate the following commands. Don’t simply copy and paste. I am making the assumption that you are installing on Ubuntu 10.04 (Lucid Lynx) and you are using the 64-bit version. ...

June 14, 2010 · 3 min · 466 words · Brandon Harris

Active Scaffold with Paperclip

I always recommend Active Scaffold to fellow Rails developers. There is a learning curve, but it can cut a lot of development time out of your clients custom CMS. Paperclip has pretty much dominated the Rails attachment market for the past year. My company has migrated to it, and there are countless tutorials and testimonials regarding it’s ease of use. I am assuming that you are using the latest version of Rails (2.3.5 as of this writing), Active Scaffold, and Paperclip. First we will install all the necessary components, then we will configure the rails app. ...

December 28, 2009 · 3 min · 427 words · Brandon Harris

Viewing Gem Rdoc

This is painfully obvious if you actually read about the tools that you use. I couldn’t find anything on my first couple of google searches, so hopefully this can fill in that gap: gem server Your gem server will probably start on port 8808, so go to localhost:8808 and you will see a list of your installed gems. From here on, you probably know what to do.

November 12, 2009 · 1 min · 67 words · Brandon Harris

Flickr Down, flickraw killed my app!

10/29/2009 Update Per Mael’s (Flickraw author) response, I was able to provide a better fix. FlickrawOptions = { :lazyload => true, :timeout => 2 } require 'flickraw' You can see more options in the documentation. 10/21/2009 Original Post As of the writing of this, Flickr has been down off and on for almost 2 hours. I noticed that a handful of our websites weren’t working. I traced the problem back to flickraw, there was one line in the ApplicationController: ...

October 20, 2009 · 1 min · 195 words · Brandon Harris

Nested Model Forms and attachment_fu

I think that a lot of Rails developers have moved on to Paperclip . Most of us started with attachment_fu , and I am willing to wager that we still have projects floating around using attachment_fu. I updated one of these older projects to 2.3.4, and created a new form utilizing the built in nested form handling as of 2.3. My models look like this: class Article < ActiveRecord::Base has_one :report accepts_nested_attributes_for :report end class Report < ActiveRecord::Base belongs_to :article has_attachment( :storage => :file_system, :file_system_path => 'public/reports', :max_size => 2.megabytes, :content_type => 'application/pdf' ) validates_as_attachment end The idea is that an article can have a pdf attachment. The view code for the nested form should look like this: ...

October 18, 2009 · 2 min · 306 words · Brandon Harris