0

I am new to rails and ruby and I wanted to make a small web app using a rails backend (not a good idea considering I am new to both). I am trying to conceptualize the folder structure of rails, and I am confused whether there is a file that runs everything in the folder.. or how does it work? I've used node.js and django (python) and usually I'll have a server file that imports my database and such, but with this rails setup--I am a bit overwhelmed. Any help would be great!

  • Are you referring to like `main()`? Or if you meant "importing all ruby files in a folder", check this [SO](https://stackoverflow.com/questions/735073/best-way-to-require-all-files-from-a-directory-in-ruby) – Jay-Ar Polidario Mar 03 '19 at 01:39
  • ^ TL;DR check out whether you'd want to use `require` or `require_relative` from this [SO](https://stackoverflow.com/questions/3672586/what-is-the-difference-between-require-relative-and-require-in-ruby). – Jay-Ar Polidario Mar 03 '19 at 01:42
  • Oh one more thing, `require` doesn't run load the ruby file twice. If you want a linear loading of code instead that can be loaded multiple times, use [`load`](https://stackoverflow.com/questions/3170638/how-does-load-differ-from-require-in-ruby) – Jay-Ar Polidario Mar 03 '19 at 01:44
  • @Jay-ArPolidario Yes, like a main() of some sort. Or would I make one? – import_theUser Mar 03 '19 at 01:44
  • 2
    check out Sergio's answer. Also, just to add to his answer, if you do `cat bin/rails` inside your rails project folder, you'll see the "starting point of code execution", and from here, just follow through the code/files that gets "required", if you really want to know everything linearly what gets executed from the start, though you'd probably don't need to know this very early as you are still learning. – Jay-Ar Polidario Mar 03 '19 at 01:54

1 Answers1

8

In your rails app, there is no one file that requires and runs all other files. There are a couple of files that do something like this, but they don't load everything.

Instead, rails knows where to look for information when it boots your app. Your database configuration goes to config/database.yml. Boot-time setup goes to config/initializers/. And so on. As a rails dev, you're expected to know this. Convention over configuration, they call it.

A good rails book can help with learning these conventions (what goes where).

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 1
    I understand that, maybe I should have asked the question better. For instance, if I use a mongoDB driver, then there would not be a database.yml. Instance you would create an object to connect to the database. Using this example, where would I run this db file ? How would my app folder know to run this file? – import_theUser Mar 03 '19 at 01:53
  • 1
    @import_theUser: if you wanted to use the raw driver, then you'd place that object initialization into an initializer or the environment files (config/development.rb, config/production.rb). But for a rails app, it's better to use Mongoid, which, yes, doesn't use `database.yml`, but it uses `mongoid.yml` in exactly the same way. – Sergio Tulentsev Mar 03 '19 at 01:58