Rails Split Up Seed File

My rails seed.rb has been getting huge and disorganized lately. It makes more sense to split it up into many smaller specific pieces, like users_seed.rb, games_seed.rb etc. Here is an easy way to do this:

First place all your seed files into the folder rails_app/db/seeds. seeds won’t exist so you’ll have to make it. After that start splitting up your huge seed.rb file into smaller pieces.

Now modify your seeds.rb to have only this code:

Dir[File.join(Rails.root, 'db', 'seeds', '**/*.rb')].each do |f|
require f

This will require all seed files from the folder and subdirectories. Pretty awesome. Then run rake db:seed to make sure it works.

Paperclip using Google Cloud Storage

Wow, I only make blog posts when there is little information / documentation on something and this takes the cake. I’m currently switching from AWS s3 to Google Cloud Storage because it doesn’t support CORS. People have requested it for 3 years and I don’t think it’ll ever be added.

So I switched to Google Cloud Storage and now I have to modify paperclip to upload assets there. There isn’t much documentation on this, so I had to go around searching for hours. I’m going to save you lots of headache, just copy what I wrote below:

options = {}
options[:path] = attachment_path
options[:storage] = :fog
options[:url] = CONFIG['cdn'] #example: assets.blah.com

options[:fog_credentials] = {
:google_storage_access_key_id=>CONFIG['GOOGLE_KEY'], #example: GOOGNSFE234C4f
:google_storage_secret_access_key=>CONFIG['GOOGLE_SECRET'] #example: AERG4234D43DFg4234DVDF5423sDCSDC

options[:fog_public] = true #this must be set to true or ALL assets will be private by DEFAULT
options[:fog_host] = CONFIG['HOST'] #example: http://assets.blah.com.commondatastorage.googleapis.com This can be changed to CDN
options[:fog_directory] =CONFIG['BUCKET'] #example: assets.blah.com

For the longest time all assets were being made private on Google Cloud Storage. I even changed the default ACL setting but no luck! This was a huge pain. Eventually I noticed the :fog_public wasn’t defaulting to true. So make sure :fog_public is always set to true!

Install Postgres 9.1 on Ubuntu

I made a quick script to install Postgresql. Enter in:

curl -L gist.github.com/raw/3033843 | bash

Contents below:

# curl -L gist.github.com/raw/3033843 | bash
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:pitti/postgresql
sudo apt-get update
sudo apt-get install postgresql-9.1 libpq-dev -y

#su postgres
#psql -d postgres -U postgres
#alter user postgres with password 'a';

Type in the comments afterwards.

To add a new user type in this:

postgres@hostname:~$ createuser
Enter name of role to add: username
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n

Paperclip Convert String into File

I been using paperclip to save javascript files to Amazon S3. I also been using it to save HTML5 canvas data. Now when someone saves an image or file it will be transferred to the server as a string. Unfortunately the image will be base64 text, so it must be conerted from base64 into a compatible paperclip file. To make this easy I created a simple helper method.

So say someone just uploaded an image and its in base64 text. It can be saved like so:

#define model
class User < ActiveRecord::Base
    has_attached_file :avatar
    def save_avatar(data)
        #remove image/png:base,
        data = data.split(',').pop
        self.avatar = Paperclip::string_to_file('avatar.png', 'image/png', data)
        return self.save
#in an action or something...
param[:user][:avatar] # = 'image/png:base,ASDFERG34SDF=='

Here is the code:

module Paperclip
  #converts a string into a file for paperclip to save
  # useage
  # self.avatar = Paperclip::string_to_file('bob.png', 'image/png', 'BASE64 here')
  def self.string_to_file(name, type, data)
    image = StringIO.new(data)
    image.class.class_eval { attr_accessor :original_filename, :content_type }
    image.original_filename = name
    image.content_type = type
    return image

Place that code in a file named paperclip_string.rb and put it in config/initializers so it loads when rails boots.

Can also find it on git gist.

Rails_Admin Not Working in Production

I just installed rails_admin and its pretty amazing.

Everything worked fine locally but when uploaded to heroku. It bombed. I couldn’t find any information online until I looked through logs and noticed it complained about missing assets.

So simple add this to production.rb:

config.assets.precompile += %w(rails_admin/rails_admin.js

Should be fixed.

FactoryGirl rand() issue

I was just creating a new factory and for some reason it was glitching up. I’m using mongodb and mongoid by the way.

I got this weird error:

Cannot serialize an object of class FactoryGirl::Declaration::Static into BSON.

I found out using rand() was causing the issue. Really strange.
So I just used the random_data gem and replaced rand() with Random.number().

Buying Secondary Domains

So recently I realized, all the secondary domains for my .com domains are available. I took sometime to decide if I should buy these domains now or just leave them.

If I leave them, there value will go up as my .com domain goes up. So its no big deal and its not that much money, so I buy them.

I now have .com, .net, .org for throwthegame. I also bought this shorter url, throwthega.me

Getting Email on Heroku for Free

So I signed up for heroku a while back and finally setup an app. Unfortunately there are no good email options. Sendgrid is nice but there is no inbox / email creation system. Its also limited to 200 emails a day on the free account.

I took a look at google apps and its amazing. Create up to 10 accounts, 10gb of storage, access to mailchimp and emails for any site. You only have to verify your account, I used google analytics its probably the best way. Just add the new google app engine email to your existing analytics as an admin and you’re golden.

I think google apps is so awsome and easy to work with. I do like sendgrid and would consider switching if they supplied a better system. I simply wanted a quick solution for receiving emails on my domain.

Paperclip Rename Tempfile Name

So I want my web app on heroku to create a file in ruby and save it using paperclip. The issue with this is heroku doesn’t allow file saves anywhere except the /tmp folder. So I have to use the Tempfile.open command instead of the File.open command.

This works fine except the Tempfile will append a random script to make it unique. This is annoying and looks something like this.


In order to fix this and save it again as its normal file name. I had to use a paperclip interpolation like so.

Paperclip.interpolates :image_name do |attachment, style|
attachment.instance.data_file_name = attachment.instance.data_file_name.match(/^w+.(png)/i)[0] rescue ‘untitled.png’

Put this inside your model and add this to the path/url


Problem solved. Just make sure you remove the old broken tempfile or they will conflict when these changes are made!

Paperclip: Do not use File.open

I was just using paperclip and found that all my files were being saved in the Root.root directory and not in the directory I wanted.

After some investigation I found that

model.file = File.open(‘new.js’, ‘w+’){|f| write.(‘hey’); }


Would save the file to the directory and would end up missing. I simply switched this to

file = File.new(‘new.js’, ‘r+’)


model.file = file


And all was well again.