Search code examples
rubysequel

Best way to separate Sequel model definitions into different file?


I'm using the Sequel gem in Ruby, but at the moment I have my model definitions up at the top of each of my scripts. However, as those models have changed over time, it's becoming hard to keep track of what the latest model is when I open up, say, an older file.

I'd like to keep my model definitions in it's own .rb file, but is that possible? If so, what's the best way to do this?


Solution

  • Of course it's possible. Extract the definitions into their own file (say, models.rb), and put require_relative('models') where they used to be.

    Or you can even put each model in its own file in a directory, say models/person.rb..., and then

    Dir["models/*.rb"].each { |file| require_relative(file) }