# Rake task with args and environment

**URL:** <https://www.ruby-forum.com/t/rake-task-with-args-and-environment/146061>\
**Category:** Rails\
**Created:** [August 17, 2008, 2:58am UTC](https://www.ruby-forum.com/t/rake-task-with-args-and-environment/146061 "2008-08-17T02:58:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [August 17, 2008, 2:58am UTC](https://www.ruby-forum.com/t/rake-task-with-args-and-environment/146061/1 "2008-08-17T02:58:50Z")

</div>

Hi all,

I need to create a rake task that both receives the environment and  
some command line arguments (as per  
[http://rubyforge.org/pipermail/rake-devel/2007-December/000352.html](http://rubyforge.org/pipermail/rake-devel/2007-December/000352.html))

For example, creating a demo user:

namespace :db do  
namespace :user do  
desc ‘Create demo user’  
task :create :username, :first\_name, :last\_name, :password do |t,  
args|  
puts “login =\> #{args[:login]}”  
puts “first\_name =\> #{args[:first\_name]}”  
puts “last\_name =\> #{args[:last\_name]}”  
puts “password =\> #{args[:password]}”  
end  
end

Works fine, printing out the arguments. However, I need access to the  
rails environment ( =\> :environment) in order to user my AR models.

What would be the correct syntax to accomplish this? I was trying  
something like the line below, but no go.

task :create :username, :first\_name, :last\_name, :password  
=\> :environment do … end

Thanks,  
-Harold

---

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [August 17, 2008, 4:21am UTC](https://www.ruby-forum.com/t/rake-task-with-args-and-environment/146061/2 "2008-08-17T04:21:03Z")

</div>

Harold wrote:

> Works fine, printing out the arguments. However, I need access to the  
> rails environment ( =\> :environment) in order to user my AR models.
> 
> What would be the correct syntax to accomplish this? I was trying  
> something like the line below, but no go.

Hi Harold,

Try ENV[‘RAILS\_ENV’]

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [August 17, 2008, 4:59am UTC](https://www.ruby-forum.com/t/rake-task-with-args-and-environment/146061/3 "2008-08-17T04:59:17Z")

</div>

Thanks Fred, that did it.

On Aug 16, 10:20 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [August 17, 2008, 4:21am UTC](https://www.ruby-forum.com/t/rake-task-with-args-and-environment/146061/4 "2008-08-17T04:21:17Z")

</div>

On 17 Aug 2008, at 01:58, Harold wrote:

> Hi all,
> 
> I need to create a rake task that both receives the environment and  
> some command line arguments (as per [http://rubyforge.org/pipermail/rake-devel/2007-December/000352.html](http://rubyforge.org/pipermail/rake-devel/2007-December/000352.html))

[snip]

> Works fine, printing out the arguments. However, I need access to the  
> rails environment ( =\> :environment) in order to user my AR models.
> 
> What would be the correct syntax to accomplish this? I was trying  
> something like the line below, but no go.

Read a little further from the url you linked:

> task :upload, :file1, :file2, :needs =\> [:pre\_upload] do … end

Fred

---

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [August 17, 2008, 1:36pm UTC](https://www.ruby-forum.com/t/rake-task-with-args-and-environment/146061/5 "2008-08-17T13:36:04Z")

</div>

Harold wrote:

> Thanks Fred, that did it.
> 
> On Aug 16, 10:20ï¿½pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

And this is exactly why I preface many of my posts with “If I understand  
correctly…”. I obviously did _not_ understand correctly this time.

Peace,  
Phillip
