Skip to content

Instantly share code, notes, and snippets.

@stim371
Created November 30, 2012 22:23
Show Gist options
  • Save stim371/4179131 to your computer and use it in GitHub Desktop.
Save stim371/4179131 to your computer and use it in GitHub Desktop.
fixing deprecations from Rails 2 to Rails 3

This is a catalog of the various deprecation notices that I worked through during my upgrade to Rails 3.

Rails Constants for env and root

###Details These constants have been phased out in favor of attributes on the Rails module

# Rails 2
if RAILS_ENV = 2.3
# OR
RAILS_ROOT

# Rails 3
Rails.env
Rails.root

# The second is particularly intersting because you can build the path a little more cleanly
Rails.root.join('lib', 'file.rb') #=> /Users/name/../lib/file.rb

###Caveat Make sure if you do a find/replace to not replace instances where those constants are actually strings. These are still valid calls to environment variables set.

Some parsers still mistake this as deprecated since they simply do a text search.

ENV['RAILS_ENV'] #=> this is still valid in Rails 3. Don't overwrite it!

Custom db attributes in models

The #set_primary_key and #set_table_name methods found in models are deprecated in 3.2

# Rails 2
set_primary_key :custom_id
set_table_name :new_table

#Rails 3
self.primary_key 'custom_id'
self.table_name 'new_table'

Rails logger auto_flushing

Looks like you need to handle this elsewhere if it's really an issue

http://guides.rubyonrails.org/3_2_release_notes.html

Whitelist attributes

The new default in Rails 3 is config.active_record.whitelist_attributes = true in application.rb, which protects all attributes from mass assignment by default.

In order to expose model attributes to mass assignment from methods like update_attributes or create, you need to add them to attr_accessible on the model.

Workaround

If, for some reason, you would like to use those methods without attr_accessible, you can include an option as a second parameter to bypass the filter.

@user.update_attributes({
  :name => 'Mike',
  :email => 'mike@jones.com'
  },
  :without_protection => true)

ActionMailer changes

###Dynamic sender with method_missing is removed

  • In Rails 2, mail methods took the form of deliver__methodname_ and method_missing parsed it all out.
  • In Rails 3, we still build a method in a mailer file, but the method returns a Mail object, which we will call .deliver on.

#mail method for attributes

  • In Rails 2, we simply assigned all our variables in the method, but didn't do any additional setup.
  • In Rails 3, we switched parameters over to instance variables and moved mail-related parameters into a #mail method.
    • New mail method (link)
    • Instance variables are now defined as such in views. (i.e., @stuff_to_expose_in_views = randomvar)

Recipient vs to? (2 vs 3?)

  • Rails 2 uses recipient to collect the emails the message should be sent to.
  • Rails 3's mail method has a :to key that serves the same purpose.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment