Rake aborted! ActiveRecord::ConnectionNotEstablished


#1

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. :slight_smile:
Joshua


#2

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


#3

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…


#4

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

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?


#5

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

#6

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… :wink: