Understanding How Rails Works by
Building It Small
〜 mini-rails 〜
1
Slide 2
Slide 2 text
Nobo
I work at SonicGarden Inc.
I organize Seto.rb.
Also organizing 沖縄Ruby会議03 !(Feb 20 2027)
@Hitoshi-Noborikawa
2
Slide 3
Slide 3 text
How It Started
3
Slide 4
Slide 4 text
The more I used Rails every day, the more I wondered what was happening inside.
What is Rails.application.routes.draw?
What does where actually do?
When is a view loaded?
I know — let's read the Rails source.
4
Slide 5
Slide 5 text
I tried to look up the definition of where, but...
So many methods I didn't understand: include, extend, included, define_method...
Too early for me. Time to come back later.
5
Slide 6
Slide 6 text
Expanding what I could read, bit by bit
Metaprogramming
Reading the source code of small gems
Reading the code of projects I work on, closely
Maybe I can do it now?
6
Slide 7
Slide 7 text
Reading got easier, but Rails is still too big
I understand the parts, but not the whole picture
What if I build a small one to see the whole picture?
7
Slide 8
Slide 8 text
mini-rails
8
Slide 9
Slide 9 text
About mini-rails
A framework I built myself, modeled mainly on Rails 1–3.
Strictly a learning project.
I'll publish it on GitHub after this talk.
9
Slide 10
Slide 10 text
What This Talk Covers
Walking through Request → Response with a mini blog running on mini-rails.
The focus is on the mini-rails implementation.
10
rackup → run MiniRails.application
Rails: the Application sits on top of a middleware stack
mini-rails: only the bare minimum middleware
Reference: railties/lib/rails/application.rb
16
Post.order goes through all
Rails: delegate(*QUERYING_METHODS, to: :all)
No delegate here — order, where and find all go through define_method
Reference: activerecord/lib/active_record/querying.rb
29
Slide 30
Slide 30 text
all creates a new Relation
If it were reused, A's conditions would leak into B
Rails: all is current_scope || default_scoped — default_scope and scoping feed in
mini-rails: just returns a new Relation
30
Slide 31
Slide 31 text
Stacks up the Relation
31
Slide 32
Slide 32 text
Builds the SQL
Rails: builds an Arel AST, and a Visitor turns it into SQL
mini-rails: just concatenates strings
32
Slide 33
Slide 33 text
@post.save
33
Slide 34
Slide 34 text
save
create_record is called for a new record
valid? runs validations and run_callbacks runs before/after callbacks, but I'll skip those here
34
Renders the view
Rails splits this into Callbacks and ImplicitRender; mini-rails has only this method
Reference: actionpack/lib/action_controller/metal/implicit_render.rb
37
Slide 38
Slide 38 text
Calls the Renderer
38
Slide 39
Slide 39 text
Reads the template, passes it to ERB
Rails: caches templates
mini-rails: reads them on every request
Reference: actionview/lib/action_view/template.rb
39
Slide 40
Slide 40 text
The action's HTML is a block; the layout's yield calls it
40
Slide 41
Slide 41 text
Response
Rack → Router → Controller → View → Response
Rails: returns it via Response#to_a
mini-rails: named to_rack_response for clarity
Reference: actionpack/lib/action_dispatch/http/response.rb
41
Post works without any require
Autoloaded at boot
Rails: Zeitwerk also uses Ruby's autoload, and reloads on change
mini-rails: same idea, once at boot — no reloading
44
Slide 45
Slide 45 text
before_action
Appended to an array when the class loads, then run per request via only / except
Rails: AbstractController::Callbacks, which also controls conditions and execution order
mini-rails: just runs an array of hashes
45
Slide 46
Slide 46 text
Features I Couldn't Cover
- MiniSupport::Concern
- Migration
- Validation
- HashWithIndifferentAccess
- MiniPack::Flash
- MiniView::Helpers
- MiniView::Partial
46
Slide 47
Slide 47 text
What to Do After This Talk
Read mini-rails
Next read the Rails 1.x implementation
https://github.com/Hitoshi-Noborikawa/mini-rails
47