Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

up/down methods convention in Rails migration #815

Closed
wants to merge 1 commit into from

2 participants

@mikong

I just noticed that in the README, the sample code for Rails migration still uses the Class methods (self.up/self.down) instead of simply just the instance methods (up/down). I updated the 2 code blocks in README, as well as the 2 generator templates in the source code.

I'm not exactly sure why there are 2 templates in the source code. But it seems to me the one under lib/ is somewhat incomplete (USAGE is not descriptive).

Lastly, I think the standard in Rails migration class naming is to refer to the table name instead of the model class name (e.g. AddSomeColumnToUsers refer to table name, while AddSomeColumnToUser refers to the model class name). I didn't change this in my commit because other people may not agree with this.

@sikachu
Owner

Thank you for sending this in. However, Rails 3.0 doesn't support this new up/down method migration, so we had to keep it as class method for the time being.

@sikachu sikachu closed this
@sikachu
Owner

Oh, by the way, I think the table name in the migration should be pluralize. Can you make that change and send another pull request?

@mikong

You're referring to the class name of the migration, right? Sure, I can make that change.

Do you know why there are 2 templates? There's one in generators/paperclip/templates and another in lib/generators/paperclip/templates.

@sikachu
Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 7, 2012
  1. @mikong
This page is out of date. Refresh to see the latest.
View
10 README.md
@@ -96,14 +96,14 @@ In your model:
In your migrations:
- class AddAvatarColumnsToUser < ActiveRecord::Migration
- def self.up
+ class AddAvatarColumnsToUsers < ActiveRecord::Migration
+ def up
change_table :users do |t|
t.has_attached_file :avatar
end
end
- def self.down
+ def down
drop_attached_file :users, :avatar
end
end
@@ -323,11 +323,11 @@ has an attribute named fingerprint. Following the user model migration example
above, the migration would look like the following.
class AddAvatarFingerprintColumnToUser < ActiveRecord::Migration
- def self.up
+ def up
add_column :users, :avatar_fingerprint, :string
end
- def self.down
+ def down
remove_column :users, :avatar_fingerprint
end
end
View
4 generators/paperclip/templates/paperclip_migration.rb.erb
@@ -1,5 +1,5 @@
class <%= migration_name %> < ActiveRecord::Migration
- def self.up
+ def up
<% attachments.each do |attachment| -%>
add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_name, :string
add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_content_type, :string
@@ -8,7 +8,7 @@ class <%= migration_name %> < ActiveRecord::Migration
<% end -%>
end
- def self.down
+ def down
<% attachments.each do |attachment| -%>
remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_name
remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_content_type
View
4 lib/generators/paperclip/templates/paperclip_migration.rb.erb
@@ -1,5 +1,5 @@
class <%= migration_class_name %> < ActiveRecord::Migration
- def self.up
+ def up
<% attachment_names.each do |attachment| -%>
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_name, :string
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_content_type, :string
@@ -8,7 +8,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration
<% end -%>
end
- def self.down
+ def down
<% attachment_names.each do |attachment| -%>
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_name
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_content_type
Something went wrong with that request. Please try again.