Tag: Rails3.1

Upgrading to Rails 3.2.0 from Rails 3.1.3

Yup, finally it's here! :) Switching Susanoo (Senpuu v5) to 3.2 wasn't so hard. Took me about 5 hours. Why so long? Well because I had to fix my "special" Paperclip version and make so additional table names changes. All the steps are presented below. Enjoy.

Gemfile

gem "rails", '3.2.0'

group :assets do
  gem 'sass-rails', '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3', :require => 'uglifier'
end

Weird stuff

rename hash column hash is defined by ActiveRecord

I've got some problems with my attributes names. Looks like, you cannot use "hash" as an attribute name. Also weird stuff was happening with my "email_change" attribute. I could write to it but I could not retrieve values (always nil). Renaming those two attributes fixed problems. So just:

        rename_column :users, :email_change, :changed_at

Small fixes

Instead of:

set_table_name :table

use:

self.table_name= :table

Templates

Passing the format in the template name is deprecated. 
Please pass render with :formats => [:html] instead

When using render with a template we've been passing a template format. For example:

render "/shared/404.html", :status => 404, :layout => false

Now we don't need to provide the file format, instead we just do it like this:

render "/shared/404", :formats => [:html], :status => 404, :layout => false

Form builder doesn't accept range as a param :(

Don't know why this is not supported. When building forms, we cannot pass a range. Instead we must pass an array. So to fix it just perform to_a on a range:

# Won't work:
1..31
# Works like a charm:
(1..31).to_a

Write inheritable attribute

Last but not least, write_inheritable_attribute was removed so now instead of:

write_inheritable_attribute(:attribute_name, value)

we should use:

class_attribute :attribute_name
self.attribute_name = value

Handling large seed files in Ruby on Rails

Sometimes seed files can get messy and big. It can be real pain it the ass to manage them. Here is fast way to split single seeds.rb file:

  1. Create directory called seeds in your db/ directory (mkdir ./db/seeds)
  2. Remove all stuff from seeds.rb and move it into your newly created files under db/seeds/ directory (put them accordingly to your own app logic)
  3. Paste code presented below into seeds.rb file
  4. Run rake db:seed

Seeds.rb file source code:

# coding: utf-8

%w{
  filename1 filename2 filename3...filenameN
}.each do |part|
  require File.expand_path(File.dirname(__FILE__))+"/seeds/#{part}.rb"
end

Why haven't I use auto-include and instead I've listed all the files? Well I wanted to maintain my seed parts load order so those parts will be loaded accordingly to my order (not based on file names).

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑