Blankman
Blankman

Reputation: 266978

How to setup activerecord + models in a file, then re-use it in all other script files?

I have a bunch of different .rb command line script files that perform various tasks, but they all use the same database.

I'm coding the 2nd .rb file, and the 1st one has all references to ActiveRecord, db connections, and classes that represent my models etc.

Is it possible to move all this to another file, and then just import the file in each of my .rb files?

How would this work?

Upvotes: 1

Views: 79

Answers (2)

the Tin Man
the Tin Man

Reputation: 160551

You need to read Modules, which is part of the "Pickaxe Book", AKA Programming Ruby.

Upvotes: 1

Matt Briggs
Matt Briggs

Reputation: 42178

require basically goes out and runs another file in the context of the current ruby process if it hasn't be required yet. if your db file is called models.rb and it is living in a sub directory called lib, it would look like this

require 'lib/models'

Upvotes: 2

Related Questions