Rake question

I've searched this group and google, but have not found the answer to my question. If there is a post about it somewhere, I'd greatly appreciate a link. I also started to dig into the rake code, but, frankly, didn't understand it.

Question:

Why does it seem that :environment is required in order to have ActiveRecord::Base.configurations populated with the contents of database.yml.

Explanation:

namespace :phillip do   task :mytask do     ActiveRecord::Base.configurations.each_value do |config|       p config['database']     end   end end

prints nothing. Yet

namespace :phillip do   task :mytask => :environment do     ActiveRecord::Base.configurations.each_value do |config|       p config['database']     end   end end

outputs the database names in my database.yml file.

I understand that if I'm actually trying to do something, I'd need the environment. But it seems that the configurations would be available without knowing a specific environment.

Peace, Phillip

:environment is a task that loads up environment.rb.

When you do

task :foo => :environment

you’re saying “my task depends on another task called :environment, so invoke that first , and then do my stuff”

prints nothing. Yet

namespace :phillip do task :mytask => :environment do    ActiveRecord::Base.configurations.each_value do |config|      p config['database']    end end end

outputs the database names in my database.yml file.

I understand that if I'm actually trying to do something, I'd need the environment. But it seems that the configurations would be available without knowing a specific environment.

Well at a very basic level, ActiveRecord::Base.configurations isn't
going to populate itself. rails isn't even loaded if your task doesn't
depend on :environment

Fred

Frederick Cheung wrote:

Well at a very basic level, ActiveRecord::Base.configurations isn't going to populate itself. rails isn't even loaded if your task doesn't depend on :environment

Fred

Thanks, Fred. I'm understanding now. I haven't ventured into rake until now and didn't get it. I read through some of the rake docs, but apparently didn't actually digest anything.

Phillip

Thanks for that Brian I didn't know either!