# Rake aborted! ActiveRecord::ConnectionNotEstablished

**URL:** <https://www.ruby-forum.com/t/rake-aborted-activerecord-connectionnotestablished/60716>\
**Category:** Rails\
**Created:** [May 16, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/rake-aborted-activerecord-connectionnotestablished/60716 "2006-05-16T23:48:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [May 16, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/rake-aborted-activerecord-connectionnotestablished/60716/1 "2006-05-16T23:48:19Z")

</div>

Hi all

I’m just learning to use rake and ran into the following problem:

chraftbuech:/Users/Josh/Webwork/CommentingSystem josh$ rake  
create\_test\_news  
(in /Users/Josh/Webwork/CommentingSystem)  
Create test news  
rake aborted!  
ActiveRecord::ConnectionNotEstablished

The task’s code is:

desc ‘Creates some test news items with comments’  
task ‘create\_test\_news’ do  
puts ‘Create test news’  
n = NewsItem.new  
n.subject = ‘This is the first news item’  
n.body = ‘After days of work this is the first news item ever  
published using the NewsEngine. Have fun with it and add some comments!  
:-)’  
raise ‘Could not save first news item!’ if !n.save  
end

Can you tell me what I have to do to establish the connection? I guess  
somehow Rails doesn’t connect to MySQL?

Thanks for help. 🙂  
Joshua

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [May 17, 2006, 1:22am UTC](https://www.ruby-forum.com/t/rake-aborted-activerecord-connectionnotestablished/60716/2 "2006-05-17T01:22:44Z")

</div>

Joshua M. wrote:

> Can you tell me what I have to do to establish the connection? I guess  
> somehow Rails doesn’t connect to MySQL?

This is probably way too obvious, but do you have your database  
connection set up in config/database.yml?

If not, that could cause it.

I got this error the other day in an especially dopey way. I was working  
on a non-database application, and had added

config.frameworks -= [ :action\_web\_service, :action\_mailer,  
:active\_record ]

to environment.rb to save memory.

Then I decided to add an OpenProfile authentication controller to it, so  
I added a database to keep track of users.

It took me two hours to figure out why I couldn’t connect to the  
database:-)

–Al Evans

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [May 17, 2006, 10:25am UTC](https://www.ruby-forum.com/t/rake-aborted-activerecord-connectionnotestablished/60716/3 "2006-05-17T10:25:50Z")

</div>

Al Evans wrote:

> Joshua M. wrote:
> 
> > Can you tell me what I have to do to establish the connection? I guess  
> > somehow Rails doesn’t connect to MySQL?
> 
> This is probably way too obvious, but do you have your database  
> connection set up in config/database.yml?  
> It’s correctly set up, because over the console I can create the items  
> without any problems… :-/ there’s something wrong with rake, I  
> guess…

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [May 17, 2006, 10:32am UTC](https://www.ruby-forum.com/t/rake-aborted-activerecord-connectionnotestablished/60716/4 "2006-05-17T10:32:30Z")

</div>

LOL funny that I can’t edit my posts here… 😉

It was really that I have to add the dependence :environment to the  
task:

desc ‘Creates some test news items with comments’  
task :create\_test\_news =\> :environment do  
puts ‘Create test news’  
n = NewsItem.new  
n.subject = ‘This is the first news item’  
n.body = ‘After days of work this is the first news item ever  
published using the NewsEngine. Have fun with it and add some comments!  
:-)’  
raise ‘Could not save first news item!’ if !n.save  
end

But still the question: where is this task defined?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [May 17, 2006, 10:45am UTC](https://www.ruby-forum.com/t/rake-aborted-activerecord-connectionnotestablished/60716/5 "2006-05-17T10:45:45Z")

</div>

The :environment task comes from Rails’ own Rakefiles - so either in  
the gems, or in your copy of Rails in vendor  
(RAILS\_ROOT/vendor/rails/railties/lib/tasks/misc.rake, I believe)

- james

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [May 17, 2006, 10:29am UTC](https://www.ruby-forum.com/t/rake-aborted-activerecord-connectionnotestablished/60716/6 "2006-05-17T10:29:30Z")

</div>

I just remarked that every task in the UserEngine engine is dependent on  
:environment:

desc ‘Create the default roles’  
task :create\_roles =\> :environment do  
…  
end

Do I need to add this dependence too? But where does this :environment  
task come from? It’s definitely not in my rake file… 😉
