Best Way to detect if you are in a migration?

I have a method where I would like output to occur via “puts” only if
its currently in the middle of a rake task or if its in the middle of a
migration.

How can I detect if the program execution is in the context of a
migration or a rake task?

Anybody?

On Aug 14, 3:02 am, Aryk G. [email protected]
wrote:

I have a method where I would like output to occur via “puts” only if
its currently in the middle of a rake task or if its in the middle of a
migration.

How can I detect if the program execution is in the context of a
migration or a rake task?

Posted viahttp://www.ruby-forum.com/.

Not sure about this, but I think calling self.class or self.parent
inside a migration task (self.up and self.down) should return
ActiveRecord::Migration.

I im interested in this question, has somebody the answer?

right, but Im talking about deep in the code, like in an ActiveRecord
model lets say.

I recently posted a similar question. No answer…

http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/5d38a62129fa63e4/a78671aa145bf278?hl=en#a78671aa145bf278

On Feb 9, 3:11 pm, Frantisek P. [email protected]

Hey guys, i’ve not tested it, but:

  1. you are able to set environment variable when runing rake from
    commandline

  2. you are able to test against ENV[:variable] in your code.

  3. problem solved? :slight_smile:

here is the inspiration:

http://ryandaigle.com/articles/2007/6/22/using-command-line-parameters-w-rake-and-capistrano

i once had a similar problem. in my case i wanted to skip some code
whenever it is called from a rake task. back then i solved it the way
Frantisek P. proposed. still working, no problems. just set your
ENV-variable at the beginning of your rake task.

Create your own class that inherits ActiveRecord::Migration and use
it, defining the behavior you would like to have.

Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)

On Mon, Feb 9, 2009 at 5:11 PM, Frantisek P.