Rake task with args and environment

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)

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

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

Thanks Fred, that did it.

On Aug 16, 10:20 pm, Frederick C. [email protected]

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)

[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

Harold wrote:

Thanks Fred, that did it.

On Aug 16, 10:20�pm, Frederick C. [email protected]

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs