开发者

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 thi开发者_开发百科s work?


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'


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜