Great stuff I tweeted on 2009-02-25

This entry was posted February 25 2009 under Uncategorized.

No comments
  • Government buildings in hamilton. So depressing, but this greeter is strangely cheery. #

Great stuff I tweeted on 2009-02-24

This entry was posted February 24 2009 under Uncategorized.

No comments
  • Twitter Box, the litter box that tells you when your cat is pooping. #
  • I’ve got more smoked salmon than I can shake a stick at. Anyone have any good recipes? #
  • Extra strength Tums are just bigger. That’s not extra strength, that’s just extra Tums. #

Great stuff I tweeted on 2009-02-23

This entry was posted February 23 2009 under Uncategorized.

No comments
  • If you hadn’t written your article about Web 2.0 by now, it’s too late. You missed your chance. You hear me, Canadian Information Weekly? #

Great stuff I tweeted on 2009-02-21

This entry was posted February 21 2009 under Uncategorized.

No comments
  • New rule: If you’ve never built a website, you are not qualified to design a website #
  • Would anyone notice if I slipped a few lines into the stimulus package declaring use of Internet Explorer 6 “terrorist activity”? #

Great stuff I tweeted on 2009-02-20

This entry was posted February 20 2009 under Uncategorized.

No comments
  • Norway: A progressive society even among browser choice http://tinyurl.com/cgws6e #
  • The foot pedal is under utilized in modern computing. Think of the time it would save! #
  • ATTN PRINT DESIGNERS DOING WEB: Web pages are not 8×11. They do not have a fixed amount of content. The Internet is not a magazine! #
  • Blog post: “Unable to Open Database” with Sqlite3 (simple fix to an annoying error) http://tinyurl.com/c67dmg #
  • Good lord yes. Bacon Cheese Pizza Burger http://i42.tinypic.com/2n7kx81.jpg #

“Unable to Open Database” with Sqlite3

This entry was posted February 19 2009 under *nix, Errors.

3 comments

Just spent an hour or so trying to figure out why my Sinatra app running DataMapper couldn’t write to my sqlite database, and kept returning the vague error “Unable to Open Database”.  Turns out the problem is as simple as making sure that the user running the script not only has write permissions on the database file, but also on the directory that the datbase file is in.  If you find yourself getting this error and you can’t figure out why, that’s probably the problem!

Great stuff I tweeted on 2009-02-19

This entry was posted February 19 2009 under Uncategorized.

No comments
  • Quick Rails Tutorial: Overriding Paperclip Defaults for your Entire Rails App http://tinyurl.com/at7ujd #
  • In 2005/2006 I spent about 25 days total play time on Counter Strike and Day of Defeat… and I don’t even really regret it #

Overriding Paperclip Defaults for your Entire Rails App

This entry was posted February 18 2009 under Rails, Web Development.

1 comment

Paperclip is hands-down the best file attachment plugin for Rails.  File_column was alright a while back, and attachment_fu was a frustrating mess.  One thing I don’t like about Paperclip though is that by default it fills your public folder with uploads.  (NOTE: As of the latest version of Paperclip this is no longer true.) This is even worse if you have an attachment called “images” as they just get dropped into your images folder.

While it’s fairly easy to redefine where the attachments get saved on a per-model basis with the :url and :path options, I very much prefer to do something once and then never have to think about it again.  (This preference is part of the reason that this is only my second post on a blog I’ve had since December.)  Thankfully, overriding the defaults for your entire app is easy.  Just make a file called paperclip_defaults.rb in your config/initializers directory and fill it with the following lovely code:

module Paperclip
  class Attachment
    def self.default_options
      @default_options ||= {
        :url           => "/system/:class/:attachment/:id/:style/:basename.:extension",
        :path          => ":rails_root/public/system/:class/:attachment/:id/:style/:basename.:extension",
        :styles        => {},
        :default_url   => "/:attachment/:style/missing.png",
        :default_style => :o riginal,
        :validations   => [],
        :storage       => :filesystem
      }
    end
  end
end

Just change the options in that block to suit your fancy, and the changes will take place across your entire application.

Great stuff I tweeted on 2009-02-18

This entry was posted February 18 2009 under Uncategorized.

No comments
  • Obama should show up to every speech with one of those t-shirt bazookas, except instead of t-shirts he should shoot out deeds to homes. #
  • When you’re looking at code blocks on a blog do you prefer it to wrap or scroll? #
  • Why is uploading files with PHP always such a nightmare? If you’re not going to work you could at least return an error you jerk! #
  • I need to try to be more positive in my Reddit comments… that place brings out the worst in me #

Targetting associated tables in an ActiveRecord conditions hash

This entry was posted February 5 2009 under Rails, Web Development.

8 comments

Using a conditions hash isn’t always my favourite way to write finds in Rails, but in certain cases it’s an indispensible tool for building queries.  It’s especially handy when you’re building an advanced search where conditions may or may not be present.

Recently I ran into an issue where I needed to run conditions on an associated table in just such a search and I managed to find what seems to be an undocumented (or under-documented at least) feature of ActiveRecord. For the sake of using the same example as every other Rails tutorial out there, let’s assume that I’m running a search on Authors, and that an Author has_many books. All we have to do is make a hash within the conditions hash with the name of the associated table. Searching both tables is easy as:

Author.find(:conditions => {:first_name => params[:first_name], :last_name => params[:last_name], :books => { :title => params[:book_name] } }, :include => :books)

If we were writing this as an advanced search and wanted the parameters to be optional, we would break it out like this:

conditions = {}
conditions[:first_name] = params[:first_name] unless params[:first_name].blank?
conditions[:last_name] = params[:last_name] unless params[:last_name].blank?
conditions[:books] = {:title => params[:book_name]} unless params[:book_name].blank?

@authors = Author.find(:conditions => conditions, :include => :books)

Simple!

OMG Who?

I'm Sean Roberts, a 24-year-old Ruby on Rails developer currently working for Hamilton web design firm factor[e] design initiative. This is my blog about work and nerd stuff, and maybe a little bit about music and food too.

I'm On Twitter!

  • The motto for RubyGems should be "can't someone else do it?" 2010-10-25
  • New Superchunk makes me feel like I'm 17, anyone up for semi-ironically playing Mario Kart 64 and making Kraft Dinner? 2010-09-24
  • what an unfortunate (yet hilarious) name http://imgur.com/2biWk 2010-09-22
  • Hey Google, stop interrupting me when I'm trying to ask you a question! 2010-09-09
  • My new book will be called "Living in Synergy: Memoirs of an Unwed Power Couple" (with help from @EKinread of course) 2010-09-01
  • Can anyone recommend a good tailor in Hamilton? 2010-08-23
  • More updates...