Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what do def self.up, def up, def self.down, def down mean?

Can someone explain me please what the difference between the next things?

  1. def self.up
  2. def up
  3. def self.down
  4. def down
like image 552
Alon Shmiel Avatar asked Feb 07 '13 08:02

Alon Shmiel


2 Answers

self.up and up contains the code that is ran by migrations when you do rake db:migrate. self.up is an older version of up. I'm not sure when they introduced this but until 3.0, they were using self.up.

self.down and down should contain the code that reverses the effect of the up methods. so if you created a table on up, you should drop that table on down. These methods are called when running rake db:rollback.

There's actually a new method on migrations called change which is usually used if it's easy for rails to determine the opposite of what you're doing like creating tables.

like image 182
jvnill Avatar answered Oct 05 '22 23:10

jvnill


def self.up is a class method (you can send it to the class: Klass.up), def up is a normal method (you can send it to an object). Same for down.

like image 42
ckruse Avatar answered Oct 06 '22 00:10

ckruse