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?
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…
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?
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)