0

I've got a simple little rake script that is supposed to populate the database with some data that I've already put together. However, even though it looks like it should work, I get an error telling me there is no database connection. I'm still at the "development status" and RAILS_ENV=development is set. Rails is happy, but not rake...

require 'rake'

namespace :load_drugs do
  desc "Loads drugs into the tables associated with Drug Classification"
  task :from_file, [:filename] => [:environment] do |t, args|
    puts "Loading data from: #{args[:filename]}"

    File.open(args[:filename]).each_line() do |line|
      cls, generic,names = line.split("\t")
      names = names.split(",")

      cls = DrugClassification.find_or_create(cls)
    end
  end
end

I'm using rails 4 and ruby 2.1 and the sqlite3 gem. My database exists in development and rails is fine. I'm brand new to Rails 4, so it could just be a dumb rails 4 problem.

The actual error is: undefined method `find_or_create' for DrugClassification(no database connection):Class

user632657
  • 473
  • 3
  • 10

1 Answers1

0

The OP has probably "moved on" by now, but I found this while looking for the same answer in Rails 4 and since I finally found a "solution", I thought I would share it.

It seems there is a MAJOR bug in rake in Rails 4. It only responds to very simple active record queries inside rake tasks.

I found that SomeModel.find_each DOES NOT WORK, but with no other changes I found that SomeModel.all.each does work. Similarly, the old reliable "find(id)" also works. Didn't need any others so don't know what other ones don't work (probably also find_or_create does not work based on OP results)

  • My problem actually was some changes to how RAILS 4 rake works. It didn't see the definitions for the ActiveRecord objects at all. I just ended up recreating the project in RAILS 3 since I had to get the thing up in a couple of hours. – user632657 Jun 10 '14 at 21:29