This is a catalog of the various deprecation notices that I worked through during my upgrade to Rails 3.
###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!
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'
Looks like you need to handle this elsewhere if it's really an issue
http://guides.rubyonrails.org/3_2_release_notes.html
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.
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)
###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.
- In Rails 2, we simply assigned all our variables in the method, but didn't do any additional setup.
- Rails 2 ActionMailer details
- Any additional instance variables you want in your view should be defined in the
body
hash
- 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
)
- 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.