# Using ActiveRecord in a Rake Task

**URL:** <https://www.ruby-forum.com/t/using-activerecord-in-a-rake-task/76838>\
**Category:** Rails\
**Created:** [November 21, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-rake-task/76838 "2006-11-21T17:31:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 21, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-rake-task/76838/1 "2006-11-21T17:31:52Z")

</div>

I have the following skeleton for a rake task:

==========  
namespace :db do

desc “Populate database for first time based on export from AS/400  
system”  
task :populate\_from\_export do  
ActiveRecord::Base.establish\_connection(ActiveRecord::Base.configurations[‘development’])  
User.find(:all)  
puts “hello world”  
end

# end

I want to be able to call User.find(:all) from my rake task but I am  
getting the following error:

development database is not configured

If I get rid of the first line of the task I get the following error:

ActiveRecord::ConnectionNotEstablished

So how do I make a proper conneection and make my models accessible to  
me for a rake task?

Thanks for your help :-).

Your Friend,

John

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 21, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-rake-task/76838/2 "2006-11-21T19:14:21Z")

</div>

On 11/21/06, John K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have the following skeleton for a rake task:
> 
> ==========  
> namespace :db do
> 
> desc “Populate database for first time based on export from AS/400 system”  
> task :populate\_from\_export do

You need to make sure your config files are loaded. Try:

task :populate\_from\_export =\> [:environment] do

instead of the line above my comment.

Isak

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 21, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-rake-task/76838/3 "2006-11-21T21:50:58Z")

</div>

Perfect… thanks :-).

On 11/21/06, Isak H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['development'])
> > 
> > ```
> > 
> > development database is not configured  
> > Your Friend,
> > 
> > >
> 
> >

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)
