Katie H
Katie H

Reputation: 2293

Rails - uninitialized constant Post (NameError)

I have a script in my lib folder called flow.rb This file is run as a background process.

In my file I am trying to create posts:

@post = Post.new

But I get this error

uninitialized constant Post (NameError)

What is causing this. Do I have to import or require activerecord?

Upvotes: 0

Views: 769

Answers (1)

spickermann
spickermann

Reputation: 106932

You have two options. Start you script with the rails runner:

rails runner flow.rb

Or require the Rails environment in your script with:

require File.expand_path('../config/environment', __FILE__)

Upvotes: 1

Related Questions