Where to put require 'gem' statements?

Hello,

I’ve seen all sorts of places where people put their ‘require’
statements,
and I’m not sure what the best solution is. I’m currently using it
directly
in a model/csv_processor.rb:

require ‘fastercsv’

class CsvProcessor
def self.create_csv_table(file)

FCSV.parse(…

end

I’m calling this from controllers/loader_controller.db:

my_csv_table = CsvProcessor.create_csv_table(params[:file_ref])

Works perfectly. But generally, where is the place to put all those
require statements? Is it in config/environment.rb? Would it be correct
to add them just at the end of this file?

I already tried this with another “processor”, which needs the
ruby-plsql
gem. So I added at the end of environment.rb the statements:

require ‘ruby-plsql’
plsql.activerecord_class = ActiveRecord::Base
plsql.connection.autocommit = false

But this didn’t work. I couldn’t start webrick anymore. Having instead
the same statements at the beginning of model/plsql_processor.rb
works without problems.

Any help would be appreciated,

Martin

On Feb 19, 11:53 am, Martin B. [email protected] wrote:

Works perfectly. But generally, where is the place to put all those
require statements? Is it in config/environment.rb? Would it be correct
to add them just at the end of this file?

Well if it’s a gem then using config.gem inside config/environment.rb
is usually the way to go. You don’t want to just stick things at the
bottom of environment.rb - in production mode your app’s code gets
loaded before the bottom of environment.rb gets executed. If you put
stuff in an initializer (ie in a file in config/initializers) then it
will get run early enough.

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs