Felix
Felix

Reputation: 5619

Rails find all entries of a Model. Error?

I want to find all database entries for a Model. The code is situated in corns.rake

task (:clean_database) do
    clients = Client.all
    puts "BLAAAAAAA"
    puts clients.inspect
end

When I run this code I got this error:

rake aborted!
NameError: uninitialized constant Client

Upvotes: 0

Views: 49

Answers (2)

Max Williams
Max Williams

Reputation: 32933

You need to load your Rails environment in the rake task in order for it to know about your model classes (eg Client)

task(:clean_database => :environment) do
  ...

note also no space between task and ( or you'll get a warning about dodgy parentheses.

Upvotes: 1

Marek Lipka
Marek Lipka

Reputation: 51151

Your task needs environment to be loaded, so:

task clean_database: :environment do

Upvotes: 2

Related Questions