-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupgrading_ruby_on_rails.html
More file actions
1534 lines (1386 loc) · 109 KB
/
upgrading_ruby_on_rails.html
File metadata and controls
1534 lines (1386 loc) · 109 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Upgrading Ruby on Rails — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" data-turbolinks-track="reload">
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<script src="javascripts/syntaxhighlighter.js" data-turbolinks-track="reload"></script>
<script src="javascripts/turbolinks.js" data-turbolinks-track="reload"></script>
<script src="javascripts/guides.js" data-turbolinks-track="reload"></script>
<script src="javascripts/responsive-tables.js" data-turbolinks-track="reload"></script>
</head>
<body class="guide">
<div>
<img src="images/edge_badge.png" alt="edge-badge" id="edge-badge" />
</div>
<div id="topNav">
<div class="wrapper">
<strong class="more-info-label">Veja mais em <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
<span class="red-button more-info-button">
Mais Ruby on Rails
</span>
<ul class="more-info-links s-hidden">
<li class="more-info"><a href="https://weblog.rubyonrails.org/">Blog</a></li>
<li class="more-info"><a href="https://guides.rubyonrails.org/">Guia</a></li>
<li class="more-info"><a href="http://api.rubyonrails.org/">API</a></li>
<li class="more-info"><a href="https://stackoverflow.com/questions/tagged/ruby-on-rails">Pedir ajuda</a></li>
<li class="more-info"><a href="https://github.com/rails/rails">Contribuir no GitHub</a></li>
</ul>
</div>
</div>
<div id="header">
<div class="wrapper clearfix">
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
<ul class="nav">
<li><a class="nav-item" href="index.html">Home</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guias</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<div class="guides-section-container">
<div class="guides-section">
<dt>Comece Aqui</dt>
</div>
<div class="guides-section">
<dt>Models</dt>
</div>
<div class="guides-section">
<dt>Views</dt>
</div>
<div class="guides-section">
<dt>Controllers</dt>
</div>
<div class="guides-section">
<dt>Other Components</dt>
</div>
<div class="guides-section">
<dt>Digging Deeper</dt>
</div>
<div class="guides-section">
<dt>Extending Rails</dt>
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
<dd><a href="generators.html">Creating and Customizing Rails Generators & Templates</a></dd>
</div>
<div class="guides-section">
<dt>Contributions</dt>
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
<dd><a href="ruby_on_rails_guides_guidelines.html">Guides Guidelines</a></dd>
</div>
<div class="guides-section">
<dt>Policies</dt>
<dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
</div>
<div class="guides-section">
<dt>Release Notes</dt>
<dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
<dd><a href="5_2_release_notes.html">Version 5.2 - April 2018</a></dd>
<dd><a href="5_1_release_notes.html">Version 5.1 - April 2017</a></dd>
<dd><a href="5_0_release_notes.html">Version 5.0 - June 2016</a></dd>
<dd><a href="4_2_release_notes.html">Version 4.2 - December 2014</a></dd>
<dd><a href="4_1_release_notes.html">Version 4.1 - April 2014</a></dd>
<dd><a href="4_0_release_notes.html">Version 4.0 - June 2013</a></dd>
<dd><a href="3_2_release_notes.html">Version 3.2 - January 2012</a></dd>
<dd><a href="3_1_release_notes.html">Version 3.1 - August 2011</a></dd>
<dd><a href="3_0_release_notes.html">Version 3.0 - August 2010</a></dd>
<dd><a href="2_3_release_notes.html">Version 2.3 - March 2009</a></dd>
<dd><a href="2_2_release_notes.html">Version 2.2 - November 2008</a></dd>
</div>
</div>
</div>
</li>
<li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribua</a></li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">Guias</option>
<optgroup label="Comece Aqui">
</optgroup>
<optgroup label="Models">
</optgroup>
<optgroup label="Views">
</optgroup>
<optgroup label="Controllers">
</optgroup>
<optgroup label="Other Components">
</optgroup>
<optgroup label="Digging Deeper">
</optgroup>
<optgroup label="Extending Rails">
<option value="rails_on_rack.html">Rails on Rack</option>
<option value="generators.html">Creating and Customizing Rails Generators & Templates</option>
</optgroup>
<optgroup label="Contributions">
<option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
<option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
<option value="ruby_on_rails_guides_guidelines.html">Guides Guidelines</option>
</optgroup>
<optgroup label="Policies">
<option value="maintenance_policy.html">Maintenance Policy</option>
</optgroup>
<optgroup label="Release Notes">
<option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
<option value="5_2_release_notes.html">Version 5.2 - April 2018</option>
<option value="5_1_release_notes.html">Version 5.1 - April 2017</option>
<option value="5_0_release_notes.html">Version 5.0 - June 2016</option>
<option value="4_2_release_notes.html">Version 4.2 - December 2014</option>
<option value="4_1_release_notes.html">Version 4.1 - April 2014</option>
<option value="4_0_release_notes.html">Version 4.0 - June 2013</option>
<option value="3_2_release_notes.html">Version 3.2 - January 2012</option>
<option value="3_1_release_notes.html">Version 3.1 - August 2011</option>
<option value="3_0_release_notes.html">Version 3.0 - August 2010</option>
<option value="2_3_release_notes.html">Version 2.3 - March 2009</option>
<option value="2_2_release_notes.html">Version 2.2 - November 2008</option>
</optgroup>
</select>
</li>
</ul>
</div>
</div>
<hr class="hide" />
<div id="feature">
<div class="wrapper">
<h2>Upgrading Ruby on Rails</h2><p>This guide provides steps to be followed when you upgrade your applications to a newer version of Ruby on Rails. These steps are also available in individual release guides.</p>
<div id="subCol">
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
<ol class="chapters">
<li>
<a href="#general-advice">General Advice</a>
<ul>
<li><a href="#test-coverage">Test Coverage</a></li>
<li><a href="#the-upgrade-process">The Upgrade Process</a></li>
<li><a href="#ruby-versions">Ruby Versions</a></li>
<li><a href="#the-update-task">The Update Task</a></li>
<li><a href="#configure-framework-defaults">Configure Framework Defaults</a></li>
</ul>
</li>
<li>
<a href="#upgrading-from-rails-5-2-to-rails-6-0">Upgrading from Rails 5.2 to Rails 6.0</a>
<ul>
<li><a href="#force-ssl">Force SSL</a></li>
<li><a href="#purpose-in-signed-or-encrypted-cookie-is-now-embedded-in-the-cookies-values">Purpose in signed or encrypted cookie is now embedded in the cookies values</a></li>
</ul>
</li>
<li>
<a href="#upgrading-from-rails-5-1-to-rails-5-2">Upgrading from Rails 5.1 to Rails 5.2</a>
<ul>
<li><a href="#bootsnap">Bootsnap</a></li>
<li><a href="#expiry-in-signed-or-encrypted-cookie-is-now-embedded-in-the-cookies-values">Expiry in signed or encrypted cookie is now embedded in the cookies values</a></li>
</ul>
</li>
<li>
<a href="#upgrading-from-rails-5-0-to-rails-5-1">Upgrading from Rails 5.0 to Rails 5.1</a>
<ul>
<li><a href="#top-level-hashwithindifferentaccess-is-soft-deprecated">Top-level <code>HashWithIndifferentAccess</code> is soft-deprecated</a></li>
<li><a href="#application-secrets-now-loaded-with-all-keys-as-symbols"><code>application.secrets</code> now loaded with all keys as symbols</a></li>
</ul>
</li>
<li>
<a href="#upgrading-from-rails-4-2-to-rails-5-0">Upgrading from Rails 4.2 to Rails 5.0</a>
<ul>
<li><a href="#ruby-2-2-2-required">Ruby 2.2.2+ required</a></li>
<li><a href="#active-record-models-now-inherit-from-applicationrecord-by-default">Active Record Models Now Inherit from ApplicationRecord by Default</a></li>
<li><a href="#halting-callback-chains-via-throw-abort">Halting Callback Chains via <code>throw(:abort)</code></a></li>
<li><a href="#activejob-now-inherits-from-applicationjob-by-default">ActiveJob Now Inherits from ApplicationJob by Default</a></li>
<li><a href="#rails-controller-testing">Rails Controller Testing</a></li>
<li><a href="#autoloading-is-disabled-after-booting-in-the-production-environment">Autoloading is Disabled After Booting in the Production Environment</a></li>
<li><a href="#xml-serialization">XML Serialization</a></li>
<li><a href="#removed-support-for-legacy-mysql-database-adapter">Removed Support for Legacy <code>mysql</code> Database Adapter</a></li>
<li><a href="#removed-support-for-debugger">Removed Support for Debugger</a></li>
<li><a href="#use-rails-for-running-tasks-and-tests">Use <code>rails</code> for running tasks and tests</a></li>
<li><a href="#actioncontroller-parameters-no-longer-inherits-from-hashwithindifferentaccess"><code>ActionController::Parameters</code> No Longer Inherits from <code>HashWithIndifferentAccess</code></a></li>
<li><a href="#protect-from-forgery-now-defaults-to-prepend-false"><code>protect_from_forgery</code> Now Defaults to <code>prepend: false</code></a></li>
<li><a href="#default-template-handler-is-now-raw">Default Template Handler is Now RAW</a></li>
<li><a href="#added-wildcard-matching-for-template-dependencies">Added Wildcard Matching for Template Dependencies</a></li>
<li><a href="#actionview-helpers-recordtaghelper-moved-to-external-gem-record-tag-helper"><code>ActionView::Helpers::RecordTagHelper</code> moved to external gem (record_tag_helper)</a></li>
<li><a href="#removed-support-for-protected-attributes-gem">Removed Support for <code>protected_attributes</code> Gem</a></li>
<li><a href="#removed-support-for-activerecord-deprecated-finders-gem">Removed support for <code>activerecord-deprecated_finders</code> gem</a></li>
<li><a href="#activesupport-testcase-default-test-order-is-now-random"><code>ActiveSupport::TestCase</code> Default Test Order is Now Random</a></li>
<li><a href="#actioncontroller-live-became-a-concern"><code>ActionController::Live</code> became a <code>Concern</code></a></li>
<li><a href="#new-framework-defaults">New Framework Defaults</a></li>
<li><a href="#changes-with-json-jsonb-serialization">Changes with JSON/JSONB serialization</a></li>
</ul>
</li>
<li>
<a href="#upgrading-from-rails-4-1-to-rails-4-2">Upgrading from Rails 4.1 to Rails 4.2</a>
<ul>
<li><a href="#web-console">Web Console</a></li>
<li><a href="#responders">Responders</a></li>
<li><a href="#error-handling-in-transaction-callbacks">Error handling in transaction callbacks</a></li>
<li><a href="#ordering-of-test-cases">Ordering of test cases</a></li>
<li><a href="#serialized-attributes">Serialized attributes</a></li>
<li><a href="#production-log-level">Production log level</a></li>
<li><a href="#after-bundle-in-rails-templates"><code>after_bundle</code> in Rails templates</a></li>
<li><a href="#rails-html-sanitizer">Rails HTML Sanitizer</a></li>
<li><a href="#rails-dom-testing">Rails DOM Testing</a></li>
<li><a href="#masked-authenticity-tokens">Masked Authenticity Tokens</a></li>
<li><a href="#action-mailer">Action Mailer</a></li>
<li><a href="#foreign-key-support">Foreign Key Support</a></li>
</ul>
</li>
<li>
<a href="#upgrading-from-rails-4-0-to-rails-4-1">Upgrading from Rails 4.0 to Rails 4.1</a>
<ul>
<li><a href="#csrf-protection-from-remote-script-tags">CSRF protection from remote <code><script></code> tags</a></li>
<li><a href="#spring">Spring</a></li>
<li><a href="#config-secrets-yml"><code>config/secrets.yml</code></a></li>
<li><a href="#changes-to-test-helper">Changes to test helper</a></li>
<li><a href="#cookies-serializer">Cookies serializer</a></li>
<li><a href="#flash-structure-changes">Flash structure changes</a></li>
<li><a href="#changes-in-json-handling">Changes in JSON handling</a></li>
<li><a href="#usage-of-return-within-inline-callback-blocks">Usage of <code>return</code> within inline callback blocks</a></li>
<li><a href="#methods-defined-in-active-record-fixtures">Methods defined in Active Record fixtures</a></li>
<li><a href="#i18n-enforcing-available-locales">I18n enforcing available locales</a></li>
<li><a href="#mutator-methods-called-on-relation">Mutator methods called on Relation</a></li>
<li><a href="#changes-on-default-scopes">Changes on Default Scopes</a></li>
<li><a href="#rendering-content-from-string">Rendering content from string</a></li>
<li><a href="#postgresql-json-and-hstore-datatypes">PostgreSQL json and hstore datatypes</a></li>
<li><a href="#explicit-block-use-for-activesupport-callbacks">Explicit block use for <code>ActiveSupport::Callbacks</code></a></li>
</ul>
</li>
<li>
<a href="#upgrading-from-rails-3-2-to-rails-4-0">Upgrading from Rails 3.2 to Rails 4.0</a>
<ul>
<li><a href="#http-patch">HTTP PATCH</a></li>
<li><a href="#upgrading-from-rails-3-2-to-rails-4-0-gemfile">Gemfile</a></li>
<li><a href="#upgrading-from-rails-3-2-to-rails-4-0-vendor-plugins">vendor/plugins</a></li>
<li><a href="#upgrading-from-rails-3-2-to-rails-4-0-active-record">Active Record</a></li>
<li><a href="#active-resource">Active Resource</a></li>
<li><a href="#active-model">Active Model</a></li>
<li><a href="#action-pack">Action Pack</a></li>
<li><a href="#active-support">Active Support</a></li>
<li><a href="#helpers-loading-order">Helpers Loading Order</a></li>
<li><a href="#active-record-observer-and-action-controller-sweeper">Active Record Observer and Action Controller Sweeper</a></li>
<li><a href="#sprockets-rails">sprockets-rails</a></li>
<li><a href="#sass-rails">sass-rails</a></li>
</ul>
</li>
<li>
<a href="#upgrading-from-rails-3-1-to-rails-3-2">Upgrading from Rails 3.1 to Rails 3.2</a>
<ul>
<li><a href="#upgrading-from-rails-3-1-to-rails-3-2-gemfile">Gemfile</a></li>
<li><a href="#upgrading-from-rails-3-1-to-rails-3-2-config-environments-development-rb">config/environments/development.rb</a></li>
<li><a href="#upgrading-from-rails-3-1-to-rails-3-2-config-environments-test-rb">config/environments/test.rb</a></li>
<li><a href="#upgrading-from-rails-3-1-to-rails-3-2-vendor-plugins">vendor/plugins</a></li>
<li><a href="#upgrading-from-rails-3-1-to-rails-3-2-active-record">Active Record</a></li>
</ul>
</li>
<li>
<a href="#upgrading-from-rails-3-0-to-rails-3-1">Upgrading from Rails 3.0 to Rails 3.1</a>
<ul>
<li><a href="#gemfile">Gemfile</a></li>
<li><a href="#config-application-rb">config/application.rb</a></li>
<li><a href="#upgrading-from-rails-3-0-to-rails-3-1-config-environments-development-rb">config/environments/development.rb</a></li>
<li><a href="#config-environments-production-rb">config/environments/production.rb</a></li>
<li><a href="#upgrading-from-rails-3-0-to-rails-3-1-config-environments-test-rb">config/environments/test.rb</a></li>
<li><a href="#config-initializers-wrap-parameters-rb">config/initializers/wrap_parameters.rb</a></li>
<li><a href="#config-initializers-session-store-rb">config/initializers/session_store.rb</a></li>
<li><a href="#remove-cache-and-concat-options-in-asset-helpers-references-in-views">Remove :cache and :concat options in asset helpers references in views</a></li>
</ul>
</li>
</ol>
</div>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<h3 id="general-advice"><a class="anchorlink" href="#general-advice">1 General Advice</a></h3><p>Before attempting to upgrade an existing application, you should be sure you have a good reason to upgrade. You need to balance several factors: the need for new features, the increasing difficulty of finding support for old code, and your available time and skills, to name a few.</p><h4 id="test-coverage"><a class="anchorlink" href="#test-coverage">1.1 Test Coverage</a></h4><p>The best way to be sure that your application still works after upgrading is to have good test coverage before you start the process. If you don't have automated tests that exercise the bulk of your application, you'll need to spend time manually exercising all the parts that have changed. In the case of a Rails upgrade, that will mean every single piece of functionality in the application. Do yourself a favor and make sure your test coverage is good <em>before</em> you start an upgrade.</p><h4 id="the-upgrade-process"><a class="anchorlink" href="#the-upgrade-process">1.2 The Upgrade Process</a></h4><p>When changing Rails versions, it's best to move slowly, one minor version at a time, in order to make good use of the deprecation warnings. Rails version numbers are in the form Major.Minor.Patch. Major and Minor versions are allowed to make changes to the public API, so this may cause errors in your application. Patch versions only include bug fixes, and don't change any public API.</p><p>The process should go as follows:</p>
<ol>
<li>Write tests and make sure they pass.</li>
<li>Move to the latest patch version after your current version.</li>
<li>Fix tests and deprecated features.</li>
<li>Move to the latest patch version of the next minor version.</li>
</ol>
<p>Repeat this process until you reach your target Rails version. Each time you move versions, you will need to change the Rails version number in the <code>Gemfile</code> (and possibly other gem versions) and run <code>bundle update</code>. Then run the Update task mentioned below to update configuration files, then run your tests.</p><p>You can find a list of all released Rails versions <a href="https://rubygems.org/gems/rails/versions">here</a>.</p><h4 id="ruby-versions"><a class="anchorlink" href="#ruby-versions">1.3 Ruby Versions</a></h4><p>Rails generally stays close to the latest released Ruby version when it's released:</p>
<ul>
<li>Rails 6 requires Ruby 2.4.1 or newer.</li>
<li>Rails 5 requires Ruby 2.2.2 or newer.</li>
<li>Rails 4 prefers Ruby 2.0 and requires 1.9.3 or newer.</li>
<li>Rails 3.2.x is the last branch to support Ruby 1.8.7.</li>
<li>Rails 3 and above require Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially. You should upgrade as early as possible.</li>
</ul>
<div class="info"><p>Ruby 1.8.7 p248 and p249 have marshalling bugs that crash Rails. Ruby Enterprise Edition has these fixed since the release of 1.8.7-2010.02. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x, jump straight to 1.9.3 for smooth sailing.</p></div><h4 id="the-update-task"><a class="anchorlink" href="#the-update-task">1.4 The Update Task</a></h4><p>Rails provides the <code>app:update</code> command (<code>rake rails:update</code> on 4.2 and earlier). After updating the Rails version
in the <code>Gemfile</code>, run this command.
This will help you with the creation of new files and changes of old files in an
interactive session.</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails app:update
identical config/boot.rb
exist config
conflict config/routes.rb
Overwrite /myapp/config/routes.rb? (enter "h" for help) [Ynaqdh]
force config/routes.rb
conflict config/application.rb
Overwrite /myapp/config/application.rb? (enter "h" for help) [Ynaqdh]
force config/application.rb
conflict config/environment.rb
...
</pre>
</div>
<p>Don't forget to review the difference, to see if there were any unexpected changes.</p><h4 id="configure-framework-defaults"><a class="anchorlink" href="#configure-framework-defaults">1.5 Configure Framework Defaults</a></h4><p>The new Rails version might have different configuration defaults than the previous version. However, after following the steps described above, your application would still run with configuration defaults from the <em>previous</em> Rails version. That's because the value for <code>config.load_defaults</code> in <code>config/application.rb</code> has not been changed yet.</p><p>To allow you to upgrade to new defaults one by one, the update task has created a file <code>config/initializers/new_framework_defaults.rb</code>. Once your application is ready to run with new defaults, you can remove this file and flip the <code>config.load_defaults</code> value.</p><h3 id="upgrading-from-rails-5-2-to-rails-6-0"><a class="anchorlink" href="#upgrading-from-rails-5-2-to-rails-6-0">2 Upgrading from Rails 5.2 to Rails 6.0</a></h3><p>For more information on changes made to Rails 6.0 please see the <a href="6_0_release_notes.html">release notes</a>.</p><h4 id="force-ssl"><a class="anchorlink" href="#force-ssl">2.1 Force SSL</a></h4><p>The <code>force_ssl</code> method on controllers has been deprecated and will be removed in
Rails 6.1. You are encouraged to enable <code>config.force_ssl</code> to enforce HTTPS
connections throughout your application. If you need to exempt certain endpoints
from redirection, you can use <code>config.ssl_options</code> to configure that behavior.</p><h4 id="purpose-in-signed-or-encrypted-cookie-is-now-embedded-in-the-cookies-values"><a class="anchorlink" href="#purpose-in-signed-or-encrypted-cookie-is-now-embedded-in-the-cookies-values">2.2 Purpose in signed or encrypted cookie is now embedded in the cookies values</a></h4><p>To improve security, Rails now embeds the purpose information in encrypted or signed cookies value.
Rails can now thwart attacks that attempt to copy signed/encrypted value
of a cookie and use it as the value of another cookie.</p><p>This new embed information make those cookies incompatible with versions of Rails older than 6.0.</p><p>If you require your cookies to be read by 5.2 and older, or you are still validating your 6.0 deploy and want
to allow you to rollback set
<code>Rails.application.config.action_dispatch.use_cookies_with_metadata</code> to <code>false</code>.</p><h3 id="upgrading-from-rails-5-1-to-rails-5-2"><a class="anchorlink" href="#upgrading-from-rails-5-1-to-rails-5-2">3 Upgrading from Rails 5.1 to Rails 5.2</a></h3><p>For more information on changes made to Rails 5.2 please see the <a href="5_2_release_notes.html">release notes</a>.</p><h4 id="bootsnap"><a class="anchorlink" href="#bootsnap">3.1 Bootsnap</a></h4><p>Rails 5.2 adds bootsnap gem in the <a href="https://github.com/rails/rails/pull/29313">newly generated app's Gemfile</a>.
The <code>app:update</code> command sets it up in <code>boot.rb</code>. If you want to use it, then add it in the Gemfile,
otherwise change the <code>boot.rb</code> to not use bootsnap.</p><h4 id="expiry-in-signed-or-encrypted-cookie-is-now-embedded-in-the-cookies-values"><a class="anchorlink" href="#expiry-in-signed-or-encrypted-cookie-is-now-embedded-in-the-cookies-values">3.2 Expiry in signed or encrypted cookie is now embedded in the cookies values</a></h4><p>To improve security, Rails now embeds the expiry information also in encrypted or signed cookies value.</p><p>This new embed information make those cookies incompatible with versions of Rails older than 5.2.</p><p>If you require your cookies to be read by 5.1 and older, or you are still validating your 5.2 deploy and want
to allow you to rollback set
<code>Rails.application.config.action_dispatch.use_authenticated_cookie_encryption</code> to <code>false</code>.</p><h3 id="upgrading-from-rails-5-0-to-rails-5-1"><a class="anchorlink" href="#upgrading-from-rails-5-0-to-rails-5-1">4 Upgrading from Rails 5.0 to Rails 5.1</a></h3><p>For more information on changes made to Rails 5.1 please see the <a href="5_1_release_notes.html">release notes</a>.</p><h4 id="top-level-hashwithindifferentaccess-is-soft-deprecated"><a class="anchorlink" href="#top-level-hashwithindifferentaccess-is-soft-deprecated">4.1 Top-level <code>HashWithIndifferentAccess</code> is soft-deprecated</a></h4><p>If your application uses the top-level <code>HashWithIndifferentAccess</code> class, you
should slowly move your code to instead use <code>ActiveSupport::HashWithIndifferentAccess</code>.</p><p>It is only soft-deprecated, which means that your code will not break at the
moment and no deprecation warning will be displayed, but this constant will be
removed in the future.</p><p>Also, if you have pretty old YAML documents containing dumps of such objects,
you may need to load and dump them again to make sure that they reference
the right constant, and that loading them won't break in the future.</p><h4 id="application-secrets-now-loaded-with-all-keys-as-symbols"><a class="anchorlink" href="#application-secrets-now-loaded-with-all-keys-as-symbols">4.2 <code>application.secrets</code> now loaded with all keys as symbols</a></h4><p>If your application stores nested configuration in <code>config/secrets.yml</code>, all keys
are now loaded as symbols, so access using strings should be changed.</p><p>From:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Rails.application.secrets[:smtp_settings]["address"]
</pre>
</div>
<p>To:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Rails.application.secrets[:smtp_settings][:address]
</pre>
</div>
<h3 id="upgrading-from-rails-4-2-to-rails-5-0"><a class="anchorlink" href="#upgrading-from-rails-4-2-to-rails-5-0">5 Upgrading from Rails 4.2 to Rails 5.0</a></h3><p>For more information on changes made to Rails 5.0 please see the <a href="5_0_release_notes.html">release notes</a>.</p><h4 id="ruby-2-2-2-required"><a class="anchorlink" href="#ruby-2-2-2-required">5.1 Ruby 2.2.2+ required</a></h4><p>From Ruby on Rails 5.0 onwards, Ruby 2.2.2+ is the only supported Ruby version.
Make sure you are on Ruby 2.2.2 version or greater, before you proceed.</p><h4 id="active-record-models-now-inherit-from-applicationrecord-by-default"><a class="anchorlink" href="#active-record-models-now-inherit-from-applicationrecord-by-default">5.2 Active Record Models Now Inherit from ApplicationRecord by Default</a></h4><p>In Rails 4.2, an Active Record model inherits from <code>ActiveRecord::Base</code>. In Rails 5.0,
all models inherit from <code>ApplicationRecord</code>.</p><p><code>ApplicationRecord</code> is a new superclass for all app models, analogous to app
controllers subclassing <code>ApplicationController</code> instead of
<code>ActionController::Base</code>. This gives apps a single spot to configure app-wide
model behavior.</p><p>When upgrading from Rails 4.2 to Rails 5.0, you need to create an
<code>application_record.rb</code> file in <code>app/models/</code> and add the following content:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
</pre>
</div>
<p>Then make sure that all your models inherit from it.</p><h4 id="halting-callback-chains-via-throw-abort"><a class="anchorlink" href="#halting-callback-chains-via-throw-abort">5.3 Halting Callback Chains via <code>throw(:abort)</code></a></h4><p>In Rails 4.2, when a 'before' callback returns <code>false</code> in Active Record
and Active Model, then the entire callback chain is halted. In other words,
successive 'before' callbacks are not executed, and neither is the action wrapped
in callbacks.</p><p>In Rails 5.0, returning <code>false</code> in an Active Record or Active Model callback
will not have this side effect of halting the callback chain. Instead, callback
chains must be explicitly halted by calling <code>throw(:abort)</code>.</p><p>When you upgrade from Rails 4.2 to Rails 5.0, returning <code>false</code> in those kind of
callbacks will still halt the callback chain, but you will receive a deprecation
warning about this upcoming change.</p><p>When you are ready, you can opt into the new behavior and remove the deprecation
warning by adding the following configuration to your <code>config/application.rb</code>:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
ActiveSupport.halt_callback_chains_on_return_false = false
</pre>
</div>
<p>Note that this option will not affect Active Support callbacks since they never
halted the chain when any value was returned.</p><p>See <a href="https://github.com/rails/rails/pull/17227">#17227</a> for more details.</p><h4 id="activejob-now-inherits-from-applicationjob-by-default"><a class="anchorlink" href="#activejob-now-inherits-from-applicationjob-by-default">5.4 ActiveJob Now Inherits from ApplicationJob by Default</a></h4><p>In Rails 4.2, an Active Job inherits from <code>ActiveJob::Base</code>. In Rails 5.0, this
behavior has changed to now inherit from <code>ApplicationJob</code>.</p><p>When upgrading from Rails 4.2 to Rails 5.0, you need to create an
<code>application_job.rb</code> file in <code>app/jobs/</code> and add the following content:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
class ApplicationJob < ActiveJob::Base
end
</pre>
</div>
<p>Then make sure that all your job classes inherit from it.</p><p>See <a href="https://github.com/rails/rails/pull/19034">#19034</a> for more details.</p><h4 id="rails-controller-testing"><a class="anchorlink" href="#rails-controller-testing">5.5 Rails Controller Testing</a></h4><h5 id="extraction-of-some-helper-methods-to-rails-controller-testing"><a class="anchorlink" href="#extraction-of-some-helper-methods-to-rails-controller-testing">5.5.1 Extraction of some helper methods to <code>rails-controller-testing</code></a></h5><p><code>assigns</code> and <code>assert_template</code> have been extracted to the <code>rails-controller-testing</code> gem. To
continue using these methods in your controller tests, add <code>gem 'rails-controller-testing'</code> to
your <code>Gemfile</code>.</p><p>If you are using Rspec for testing, please see the extra configuration required in the gem's
documentation.</p><h5 id="new-behavior-when-uploading-files"><a class="anchorlink" href="#new-behavior-when-uploading-files">5.5.2 New behavior when uploading files</a></h5><p>If you are using <code>ActionDispatch::Http::UploadedFile</code> in your tests to
upload files, you will need to change to use the similar <code>Rack::Test::UploadedFile</code>
class instead.</p><p>See <a href="https://github.com/rails/rails/issues/26404">#26404</a> for more details.</p><h4 id="autoloading-is-disabled-after-booting-in-the-production-environment"><a class="anchorlink" href="#autoloading-is-disabled-after-booting-in-the-production-environment">5.6 Autoloading is Disabled After Booting in the Production Environment</a></h4><p>Autoloading is now disabled after booting in the production environment by
default.</p><p>Eager loading the application is part of the boot process, so top-level
constants are fine and are still autoloaded, no need to require their files.</p><p>Constants in deeper places only executed at runtime, like regular method bodies,
are also fine because the file defining them will have been eager loaded while booting.</p><p>For the vast majority of applications this change needs no action. But in the
very rare event that your application needs autoloading while running in
production mode, set <code>Rails.application.config.enable_dependency_loading</code> to
true.</p><h4 id="xml-serialization"><a class="anchorlink" href="#xml-serialization">5.7 XML Serialization</a></h4><p><code>ActiveModel::Serializers::Xml</code> has been extracted from Rails to the <code>activemodel-serializers-xml</code>
gem. To continue using XML serialization in your application, add <code>gem 'activemodel-serializers-xml'</code>
to your <code>Gemfile</code>.</p><h4 id="removed-support-for-legacy-mysql-database-adapter"><a class="anchorlink" href="#removed-support-for-legacy-mysql-database-adapter">5.8 Removed Support for Legacy <code>mysql</code> Database Adapter</a></h4><p>Rails 5 removes support for the legacy <code>mysql</code> database adapter. Most users should be able to
use <code>mysql2</code> instead. It will be converted to a separate gem when we find someone to maintain
it.</p><h4 id="removed-support-for-debugger"><a class="anchorlink" href="#removed-support-for-debugger">5.9 Removed Support for Debugger</a></h4><p><code>debugger</code> is not supported by Ruby 2.2 which is required by Rails 5. Use <code>byebug</code> instead.</p><h4 id="use-rails-for-running-tasks-and-tests"><a class="anchorlink" href="#use-rails-for-running-tasks-and-tests">5.10 Use <code>rails</code> for running tasks and tests</a></h4><p>Rails 5 adds the ability to run tasks and tests through <code>bin/rails</code> instead of rake. Generally
these changes are in parallel with rake, but some were ported over altogether. As the <code>rails</code>
command already looks for and runs <code>bin/rails</code>, we recommend you to use the shorter <code>rails</code>
over `bin/rails.</p><p>To use the new test runner simply type <code>rails test</code>.</p><p><code>rake dev:cache</code> is now <code>rails dev:cache</code>.</p><p>Run <code>rails</code> inside your application's directory to see the list of commands available.</p><h4 id="actioncontroller-parameters-no-longer-inherits-from-hashwithindifferentaccess"><a class="anchorlink" href="#actioncontroller-parameters-no-longer-inherits-from-hashwithindifferentaccess">5.11 <code>ActionController::Parameters</code> No Longer Inherits from <code>HashWithIndifferentAccess</code></a></h4><p>Calling <code>params</code> in your application will now return an object instead of a hash. If your
parameters are already permitted, then you will not need to make any changes. If you are using <code>map</code>
and other methods that depend on being able to read the hash regardless of <code>permitted?</code> you will
need to upgrade your application to first permit and then convert to a hash.</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
params.permit([:proceed_to, :return_to]).to_h
</pre>
</div>
<h4 id="protect-from-forgery-now-defaults-to-prepend-false"><a class="anchorlink" href="#protect-from-forgery-now-defaults-to-prepend-false">5.12 <code>protect_from_forgery</code> Now Defaults to <code>prepend: false</code></a></h4><p><code>protect_from_forgery</code> defaults to <code>prepend: false</code> which means that it will be inserted into
the callback chain at the point in which you call it in your application. If you want
<code>protect_from_forgery</code> to always run first, then you should change your application to use
<code>protect_from_forgery prepend: true</code>.</p><h4 id="default-template-handler-is-now-raw"><a class="anchorlink" href="#default-template-handler-is-now-raw">5.13 Default Template Handler is Now RAW</a></h4><p>Files without a template handler in their extension will be rendered using the raw handler.
Previously Rails would render files using the ERB template handler.</p><p>If you do not want your file to be handled via the raw handler, you should add an extension
to your file that can be parsed by the appropriate template handler.</p><h4 id="added-wildcard-matching-for-template-dependencies"><a class="anchorlink" href="#added-wildcard-matching-for-template-dependencies">5.14 Added Wildcard Matching for Template Dependencies</a></h4><p>You can now use wildcard matching for your template dependencies. For example, if you were
defining your templates as such:</p><div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
<% # Template Dependency: recordings/threads/events/subscribers_changed %>
<% # Template Dependency: recordings/threads/events/completed %>
<% # Template Dependency: recordings/threads/events/uncompleted %>
</pre>
</div>
<p>You can now just call the dependency once with a wildcard.</p><div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
<% # Template Dependency: recordings/threads/events/* %>
</pre>
</div>
<h4 id="actionview-helpers-recordtaghelper-moved-to-external-gem-record-tag-helper"><a class="anchorlink" href="#actionview-helpers-recordtaghelper-moved-to-external-gem-record-tag-helper">5.15 <code>ActionView::Helpers::RecordTagHelper</code> moved to external gem (record_tag_helper)</a></h4><p><code>content_tag_for</code> and <code>div_for</code> have been removed in favor of just using <code>content_tag</code>. To continue using the older methods, add the <code>record_tag_helper</code> gem to your <code>Gemfile</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
gem 'record_tag_helper', '~> 1.0'
</pre>
</div>
<p>See <a href="https://github.com/rails/rails/pull/18411">#18411</a> for more details.</p><h4 id="removed-support-for-protected-attributes-gem"><a class="anchorlink" href="#removed-support-for-protected-attributes-gem">5.16 Removed Support for <code>protected_attributes</code> Gem</a></h4><p>The <code>protected_attributes</code> gem is no longer supported in Rails 5.</p><h4 id="removed-support-for-activerecord-deprecated-finders-gem"><a class="anchorlink" href="#removed-support-for-activerecord-deprecated-finders-gem">5.17 Removed support for <code>activerecord-deprecated_finders</code> gem</a></h4><p>The <code>activerecord-deprecated_finders</code> gem is no longer supported in Rails 5.</p><h4 id="activesupport-testcase-default-test-order-is-now-random"><a class="anchorlink" href="#activesupport-testcase-default-test-order-is-now-random">5.18 <code>ActiveSupport::TestCase</code> Default Test Order is Now Random</a></h4><p>When tests are run in your application, the default order is now <code>:random</code>
instead of <code>:sorted</code>. Use the following config option to set it back to <code>:sorted</code>.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# config/environments/test.rb
Rails.application.configure do
config.active_support.test_order = :sorted
end
</pre>
</div>
<h4 id="actioncontroller-live-became-a-concern"><a class="anchorlink" href="#actioncontroller-live-became-a-concern">5.19 <code>ActionController::Live</code> became a <code>Concern</code></a></h4><p>If you include <code>ActionController::Live</code> in another module that is included in your controller, then you
should also extend the module with <code>ActiveSupport::Concern</code>. Alternatively, you can use the <code>self.included</code> hook
to include <code>ActionController::Live</code> directly to the controller once the <code>StreamingSupport</code> is included.</p><p>This means that if your application used to have its own streaming module, the following code
would break in production mode:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# This is a work-around for streamed controllers performing authentication with Warden/Devise.
# See https://github.com/plataformatec/devise/issues/2332
# Authenticating in the router is another solution as suggested in that issue
class StreamingSupport
include ActionController::Live # this won't work in production for Rails 5
# extend ActiveSupport::Concern # unless you uncomment this line.
def process(name)
super(name)
rescue ArgumentError => e
if e.message == 'uncaught throw :warden'
throw :warden
else
raise e
end
end
end
</pre>
</div>
<h4 id="new-framework-defaults"><a class="anchorlink" href="#new-framework-defaults">5.20 New Framework Defaults</a></h4><h5 id="active-record-belongs-to-required-by-default-option"><a class="anchorlink" href="#active-record-belongs-to-required-by-default-option">5.20.1 Active Record <code>belongs_to</code> Required by Default Option</a></h5><p><code>belongs_to</code> will now trigger a validation error by default if the association is not present.</p><p>This can be turned off per-association with <code>optional: true</code>.</p><p>This default will be automatically configured in new applications. If existing application
want to add this feature it will need to be turned on in an initializer.</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
config.active_record.belongs_to_required_by_default = true
</pre>
</div>
<h5 id="per-form-csrf-tokens"><a class="anchorlink" href="#per-form-csrf-tokens">5.20.2 Per-form CSRF Tokens</a></h5><p>Rails 5 now supports per-form CSRF tokens to mitigate against code-injection attacks with forms
created by JavaScript. With this option turned on, forms in your application will each have their
own CSRF token that is specific to the action and method for that form.</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
config.action_controller.per_form_csrf_tokens = true
</pre>
</div>
<h5 id="forgery-protection-with-origin-check"><a class="anchorlink" href="#forgery-protection-with-origin-check">5.20.3 Forgery Protection with Origin Check</a></h5><p>You can now configure your application to check if the HTTP <code>Origin</code> header should be checked
against the site's origin as an additional CSRF defense. Set the following in your config to
true:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
config.action_controller.forgery_protection_origin_check = true
</pre>
</div>
<h5 id="allow-configuration-of-action-mailer-queue-name"><a class="anchorlink" href="#allow-configuration-of-action-mailer-queue-name">5.20.4 Allow Configuration of Action Mailer Queue Name</a></h5><p>The default mailer queue name is <code>mailers</code>. This configuration option allows you to globally change
the queue name. Set the following in your config:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
config.action_mailer.deliver_later_queue_name = :new_queue_name
</pre>
</div>
<h5 id="support-fragment-caching-in-action-mailer-views"><a class="anchorlink" href="#support-fragment-caching-in-action-mailer-views">5.20.5 Support Fragment Caching in Action Mailer Views</a></h5><p>Set <code>config.action_mailer.perform_caching</code> in your config to determine whether your Action Mailer views
should support caching.</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
config.action_mailer.perform_caching = true
</pre>
</div>
<h5 id="configure-the-output-of-db-structure-dump"><a class="anchorlink" href="#configure-the-output-of-db-structure-dump">5.20.6 Configure the Output of <code>db:structure:dump</code></a></h5><p>If you're using <code>schema_search_path</code> or other PostgreSQL extensions, you can control how the schema is
dumped. Set to <code>:all</code> to generate all dumps, or to <code>:schema_search_path</code> to generate from schema search path.</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
config.active_record.dump_schemas = :all
</pre>
</div>
<h5 id="configure-ssl-options-to-enable-hsts-with-subdomains"><a class="anchorlink" href="#configure-ssl-options-to-enable-hsts-with-subdomains">5.20.7 Configure SSL Options to Enable HSTS with Subdomains</a></h5><p>Set the following in your config to enable HSTS when using subdomains:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
config.ssl_options = { hsts: { subdomains: true } }
</pre>
</div>
<h5 id="preserve-timezone-of-the-receiver"><a class="anchorlink" href="#preserve-timezone-of-the-receiver">5.20.8 Preserve Timezone of the Receiver</a></h5><p>When using Ruby 2.4, you can preserve the timezone of the receiver when calling <code>to_time</code>.</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
ActiveSupport.to_time_preserves_timezone = false
</pre>
</div>
<h4 id="changes-with-json-jsonb-serialization"><a class="anchorlink" href="#changes-with-json-jsonb-serialization">5.21 Changes with JSON/JSONB serialization</a></h4><p>In Rails 5.0, how JSON/JSONB attributes are serialized and deserialized changed. Now, if
you set a column equal to a <code>String</code>, Active Record will no longer turn that string
into a <code>Hash</code>, and will instead only return the string. This is not limited to code
interacting with models, but also affects <code>:default</code> column settings in <code>db/schema.rb</code>.
It is recommended that you do not set columns equal to a <code>String</code>, but pass a <code>Hash</code>
instead, which will be converted to and from a JSON string automatically.</p><h3 id="upgrading-from-rails-4-1-to-rails-4-2"><a class="anchorlink" href="#upgrading-from-rails-4-1-to-rails-4-2">6 Upgrading from Rails 4.1 to Rails 4.2</a></h3><h4 id="web-console"><a class="anchorlink" href="#web-console">6.1 Web Console</a></h4><p>First, add <code>gem 'web-console', '~> 2.0'</code> to the <code>:development</code> group in your <code>Gemfile</code> and run <code>bundle install</code> (it won't have been included when you upgraded Rails). Once it's been installed, you can simply drop a reference to the console helper (i.e., <code><%= console %></code>) into any view you want to enable it for. A console will also be provided on any error page you view in your development environment.</p><h4 id="responders"><a class="anchorlink" href="#responders">6.2 Responders</a></h4><p><code>respond_with</code> and the class-level <code>respond_to</code> methods have been extracted to the <code>responders</code> gem. To use them, simply add <code>gem 'responders', '~> 2.0'</code> to your <code>Gemfile</code>. Calls to <code>respond_with</code> and <code>respond_to</code> (again, at the class level) will no longer work without having included the <code>responders</code> gem in your dependencies:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# app/controllers/users_controller.rb
class UsersController < ApplicationController
respond_to :html, :json
def show
@user = User.find(params[:id])
respond_with @user
end
end
</pre>
</div>
<p>Instance-level <code>respond_to</code> is unaffected and does not require the additional gem:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
respond_to do |format|
format.html
format.json { render json: @user }
end
end
end
</pre>
</div>
<p>See <a href="https://github.com/rails/rails/pull/16526">#16526</a> for more details.</p><h4 id="error-handling-in-transaction-callbacks"><a class="anchorlink" href="#error-handling-in-transaction-callbacks">6.3 Error handling in transaction callbacks</a></h4><p>Currently, Active Record suppresses errors raised
within <code>after_rollback</code> or <code>after_commit</code> callbacks and only prints them to
the logs. In the next version, these errors will no longer be suppressed.
Instead, the errors will propagate normally just like in other Active
Record callbacks.</p><p>When you define an <code>after_rollback</code> or <code>after_commit</code> callback, you
will receive a deprecation warning about this upcoming change. When
you are ready, you can opt into the new behavior and remove the
deprecation warning by adding following configuration to your
<code>config/application.rb</code>:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
config.active_record.raise_in_transactional_callbacks = true
</pre>
</div>
<p>See <a href="https://github.com/rails/rails/pull/14488">#14488</a> and
<a href="https://github.com/rails/rails/pull/16537">#16537</a> for more details.</p><h4 id="ordering-of-test-cases"><a class="anchorlink" href="#ordering-of-test-cases">6.4 Ordering of test cases</a></h4><p>In Rails 5.0, test cases will be executed in random order by default. In
anticipation of this change, Rails 4.2 introduced a new configuration option
<code>active_support.test_order</code> for explicitly specifying the test ordering. This
allows you to either lock down the current behavior by setting the option to
<code>:sorted</code>, or opt into the future behavior by setting the option to <code>:random</code>.</p><p>If you do not specify a value for this option, a deprecation warning will be
emitted. To avoid this, add the following line to your test environment:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# config/environments/test.rb
Rails.application.configure do
config.active_support.test_order = :sorted # or `:random` if you prefer
end
</pre>
</div>
<h4 id="serialized-attributes"><a class="anchorlink" href="#serialized-attributes">6.5 Serialized attributes</a></h4><p>When using a custom coder (e.g. <code>serialize :metadata, JSON</code>),
assigning <code>nil</code> to a serialized attribute will save it to the database
as <code>NULL</code> instead of passing the <code>nil</code> value through the coder (e.g. <code>"null"</code>
when using the <code>JSON</code> coder).</p><h4 id="production-log-level"><a class="anchorlink" href="#production-log-level">6.6 Production log level</a></h4><p>In Rails 5, the default log level for the production environment will be changed
to <code>:debug</code> (from <code>:info</code>). To preserve the current default, add the following
line to your <code>production.rb</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Set to `:info` to match the current default, or set to `:debug` to opt-into
# the future default.
config.log_level = :info
</pre>
</div>
<h4 id="after-bundle-in-rails-templates"><a class="anchorlink" href="#after-bundle-in-rails-templates">6.7 <code>after_bundle</code> in Rails templates</a></h4><p>If you have a Rails template that adds all the files in version control, it
fails to add the generated binstubs because it gets executed before Bundler:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# template.rb
generate(:scaffold, "person name:string")
route "root to: 'people#index'"
rake("db:migrate")
git :init
git add: "."
git commit: %Q{ -m 'Initial commit' }
</pre>
</div>
<p>You can now wrap the <code>git</code> calls in an <code>after_bundle</code> block. It will be run
after the binstubs have been generated.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# template.rb
generate(:scaffold, "person name:string")
route "root to: 'people#index'"
rake("db:migrate")
after_bundle do
git :init
git add: "."
git commit: %Q{ -m 'Initial commit' }
end
</pre>
</div>
<h4 id="rails-html-sanitizer"><a class="anchorlink" href="#rails-html-sanitizer">6.8 Rails HTML Sanitizer</a></h4><p>There's a new choice for sanitizing HTML fragments in your applications. The
venerable html-scanner approach is now officially being deprecated in favor of
<a href="https://github.com/rails/rails-html-sanitizer"><code>Rails HTML Sanitizer</code></a>.</p><p>This means the methods <code>sanitize</code>, <code>sanitize_css</code>, <code>strip_tags</code> and
<code>strip_links</code> are backed by a new implementation.</p><p>This new sanitizer uses <a href="https://github.com/flavorjones/loofah">Loofah</a> internally. Loofah in turn uses Nokogiri, which
wraps XML parsers written in both C and Java, so sanitization should be faster
no matter which Ruby version you run.</p><p>The new version updates <code>sanitize</code>, so it can take a <code>Loofah::Scrubber</code> for
powerful scrubbing.
<a href="https://github.com/flavorjones/loofah#loofahscrubber">See some examples of scrubbers here</a>.</p><p>Two new scrubbers have also been added: <code>PermitScrubber</code> and <code>TargetScrubber</code>.
Read the <a href="https://github.com/rails/rails-html-sanitizer">gem's readme</a> for more information.</p><p>The documentation for <code>PermitScrubber</code> and <code>TargetScrubber</code> explains how you
can gain complete control over when and how elements should be stripped.</p><p>If your application needs to use the old sanitizer implementation, include <code>rails-deprecated_sanitizer</code> in your <code>Gemfile</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
gem 'rails-deprecated_sanitizer'
</pre>
</div>
<h4 id="rails-dom-testing"><a class="anchorlink" href="#rails-dom-testing">6.9 Rails DOM Testing</a></h4><p>The <a href="http://api.rubyonrails.org/v4.1/classes/ActionDispatch/Assertions/TagAssertions.html"><code>TagAssertions</code> module</a> (containing methods such as <code>assert_tag</code>), <a href="https://github.com/rails/rails/blob/6061472b8c310158a2a2e8e9a6b81a1aef6b60fe/actionpack/lib/action_dispatch/testing/assertions/dom.rb">has been deprecated</a> in favor of the <code>assert_select</code> methods from the <code>SelectorAssertions</code> module, which has been extracted into the <a href="https://github.com/rails/rails-dom-testing">rails-dom-testing gem</a>.</p><h4 id="masked-authenticity-tokens"><a class="anchorlink" href="#masked-authenticity-tokens">6.10 Masked Authenticity Tokens</a></h4><p>In order to mitigate SSL attacks, <code>form_authenticity_token</code> is now masked so that it varies with each request. Thus, tokens are validated by unmasking and then decrypting. As a result, any strategies for verifying requests from non-rails forms that relied on a static session CSRF token have to take this into account.</p><h4 id="action-mailer"><a class="anchorlink" href="#action-mailer">6.11 Action Mailer</a></h4><p>Previously, calling a mailer method on a mailer class will result in the
corresponding instance method being executed directly. With the introduction of
Active Job and <code>#deliver_later</code>, this is no longer true. In Rails 4.2, the
invocation of the instance methods are deferred until either <code>deliver_now</code> or
<code>deliver_later</code> is called. For example:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Notifier < ActionMailer::Base
def notify(user, ...)
puts "Called"
mail(to: user.email, ...)
end
end
mail = Notifier.notify(user, ...) # Notifier#notify is not yet called at this point
mail = mail.deliver_now # Prints "Called"
</pre>
</div>
<p>This should not result in any noticeable differences for most applications.
However, if you need some non-mailer methods to be executed synchronously, and
you were previously relying on the synchronous proxying behavior, you should
define them as class methods on the mailer class directly:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Notifier < ActionMailer::Base
def self.broadcast_notifications(users, ...)
users.each { |user| Notifier.notify(user, ...) }
end
end
</pre>
</div>
<h4 id="foreign-key-support"><a class="anchorlink" href="#foreign-key-support">6.12 Foreign Key Support</a></h4><p>The migration DSL has been expanded to support foreign key definitions. If
you've been using the Foreigner gem, you might want to consider removing it.
Note that the foreign key support of Rails is a subset of Foreigner. This means
that not every Foreigner definition can be fully replaced by its Rails
migration DSL counterpart.</p><p>The migration procedure is as follows:</p>
<ol>
<li>remove <code>gem "foreigner"</code> from the <code>Gemfile</code>.</li>
<li>run <code>bundle install</code>.</li>
<li>run <code>bin/rake db:schema:dump</code>.</li>
<li>make sure that <code>db/schema.rb</code> contains every foreign key definition with
the necessary options.</li>
</ol>
<h3 id="upgrading-from-rails-4-0-to-rails-4-1"><a class="anchorlink" href="#upgrading-from-rails-4-0-to-rails-4-1">7 Upgrading from Rails 4.0 to Rails 4.1</a></h3><h4 id="csrf-protection-from-remote-script-tags"><a class="anchorlink" href="#csrf-protection-from-remote-script-tags">7.1 CSRF protection from remote <code><script></code> tags</a></h4><p>Or, "whaaat my tests are failing!!!?" or "my <code><script></code> widget is busted!!"</p><p>Cross-site request forgery (CSRF) protection now covers GET requests with
JavaScript responses, too. This prevents a third-party site from remotely
referencing your JavaScript with a <code><script></code> tag to extract sensitive data.</p><p>This means that your functional and integration tests that use</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
get :index, format: :js
</pre>
</div>
<p>will now trigger CSRF protection. Switch to</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
xhr :get, :index, format: :js
</pre>
</div>
<p>to explicitly test an <code>XmlHttpRequest</code>.</p><div class="note"><p>Your own <code><script></code> tags are treated as cross-origin and blocked by
default, too. If you really mean to load JavaScript from <code><script></code> tags,
you must now explicitly skip CSRF protection on those actions.</p></div><h4 id="spring"><a class="anchorlink" href="#spring">7.2 Spring</a></h4><p>If you want to use Spring as your application preloader you need to:</p>
<ol>
<li>Add <code>gem 'spring', group: :development</code> to your <code>Gemfile</code>.</li>
<li>Install spring using <code>bundle install</code>.</li>
<li>Springify your binstubs with <code>bundle exec spring binstub --all</code>.</li>
</ol>
<div class="note"><p>User defined rake tasks will run in the <code>development</code> environment by
default. If you want them to run in other environments consult the
<a href="https://github.com/rails/spring#rake">Spring README</a>.</p></div><h4 id="config-secrets-yml"><a class="anchorlink" href="#config-secrets-yml">7.3 <code>config/secrets.yml</code></a></h4><p>If you want to use the new <code>secrets.yml</code> convention to store your application's
secrets, you need to:</p>
<ol>
<li>
<p>Create a <code>secrets.yml</code> file in your <code>config</code> folder with the following content:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
development:
secret_key_base:
test:
secret_key_base:
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
</pre>
</div>
</li>
<li><p>Use your existing <code>secret_key_base</code> from the <code>secret_token.rb</code> initializer to
set the SECRET_KEY_BASE environment variable for whichever users running the
Rails application in production mode. Alternatively, you can simply copy the existing
<code>secret_key_base</code> from the <code>secret_token.rb</code> initializer to <code>secrets.yml</code>
under the <code>production</code> section, replacing '<%= ENV["SECRET_KEY_BASE"] %>'.</p></li>
<li><p>Remove the <code>secret_token.rb</code> initializer.</p></li>
<li><p>Use <code>rake secret</code> to generate new keys for the <code>development</code> and <code>test</code> sections.</p></li>
<li><p>Restart your server.</p></li>
</ol>
<h4 id="changes-to-test-helper"><a class="anchorlink" href="#changes-to-test-helper">7.4 Changes to test helper</a></h4><p>If your test helper contains a call to
<code>ActiveRecord::Migration.check_pending!</code> this can be removed. The check
is now done automatically when you <code>require 'rails/test_help'</code>, although
leaving this line in your helper is not harmful in any way.</p><h4 id="cookies-serializer"><a class="anchorlink" href="#cookies-serializer">7.5 Cookies serializer</a></h4><p>Applications created before Rails 4.1 uses <code>Marshal</code> to serialize cookie values into
the signed and encrypted cookie jars. If you want to use the new <code>JSON</code>-based format
in your application, you can add an initializer file with the following content:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Rails.application.config.action_dispatch.cookies_serializer = :hybrid
</pre>
</div>
<p>This would transparently migrate your existing <code>Marshal</code>-serialized cookies into the
new <code>JSON</code>-based format.</p><p>When using the <code>:json</code> or <code>:hybrid</code> serializer, you should beware that not all
Ruby objects can be serialized as JSON. For example, <code>Date</code> and <code>Time</code> objects
will be serialized as strings, and <code>Hash</code>es will have their keys stringified.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class CookiesController < ApplicationController
def set_cookie
cookies.encrypted[:expiration_date] = Date.tomorrow # => Thu, 20 Mar 2014
redirect_to action: 'read_cookie'
end
def read_cookie
cookies.encrypted[:expiration_date] # => "2014-03-20"
end
end
</pre>
</div>
<p>It's advisable that you only store simple data (strings and numbers) in cookies.
If you have to store complex objects, you would need to handle the conversion
manually when reading the values on subsequent requests.</p><p>If you use the cookie session store, this would apply to the <code>session</code> and
<code>flash</code> hash as well.</p><h4 id="flash-structure-changes"><a class="anchorlink" href="#flash-structure-changes">7.6 Flash structure changes</a></h4><p>Flash message keys are
<a href="https://github.com/rails/rails/commit/a668beffd64106a1e1fedb71cc25eaaa11baf0c1">normalized to strings</a>. They
can still be accessed using either symbols or strings. Looping through the flash
will always yield string keys:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
flash["string"] = "a string"
flash[:symbol] = "a symbol"
# Rails < 4.1
flash.keys # => ["string", :symbol]
# Rails >= 4.1
flash.keys # => ["string", "symbol"]
</pre>
</div>
<p>Make sure you are comparing Flash message keys against strings.</p><h4 id="changes-in-json-handling"><a class="anchorlink" href="#changes-in-json-handling">7.7 Changes in JSON handling</a></h4><p>There are a few major changes related to JSON handling in Rails 4.1.</p><h5 id="multijson-removal"><a class="anchorlink" href="#multijson-removal">7.7.1 MultiJSON removal</a></h5><p>MultiJSON has reached its <a href="https://github.com/rails/rails/pull/10576">end-of-life</a>
and has been removed from Rails.</p><p>If your application currently depends on MultiJSON directly, you have a few options:</p>
<ol>
<li><p>Add 'multi_json' to your <code>Gemfile</code>. Note that this might cease to work in the future</p></li>
<li><p>Migrate away from MultiJSON by using <code>obj.to_json</code>, and <code>JSON.parse(str)</code> instead.</p></li>
</ol>
<div class="warning"><p>Do not simply replace <code>MultiJson.dump</code> and <code>MultiJson.load</code> with
<code>JSON.dump</code> and <code>JSON.load</code>. These JSON gem APIs are meant for serializing and
deserializing arbitrary Ruby objects and are generally <a href="http://www.ruby-doc.org/stdlib-2.2.2/libdoc/json/rdoc/JSON.html#method-i-load">unsafe</a>.</p></div><h5 id="json-gem-compatibility"><a class="anchorlink" href="#json-gem-compatibility">7.7.2 JSON gem compatibility</a></h5><p>Historically, Rails had some compatibility issues with the JSON gem. Using
<code>JSON.generate</code> and <code>JSON.dump</code> inside a Rails application could produce
unexpected errors.</p><p>Rails 4.1 fixed these issues by isolating its own encoder from the JSON gem. The
JSON gem APIs will function as normal, but they will not have access to any
Rails-specific features. For example:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class FooBar
def as_json(options = nil)
{ foo: 'bar' }
end
end
>> FooBar.new.to_json # => "{\"foo\":\"bar\"}"
>> JSON.generate(FooBar.new, quirks_mode: true) # => "\"#<FooBar:0x007fa80a481610>\""
</pre>
</div>
<h5 id="new-json-encoder"><a class="anchorlink" href="#new-json-encoder">7.7.3 New JSON encoder</a></h5><p>The JSON encoder in Rails 4.1 has been rewritten to take advantage of the JSON
gem. For most applications, this should be a transparent change. However, as
part of the rewrite, the following features have been removed from the encoder:</p>
<ol>
<li>Circular data structure detection</li>
<li>Support for the <code>encode_json</code> hook</li>
<li>Option to encode <code>BigDecimal</code> objects as numbers instead of strings</li>
</ol>
<p>If your application depends on one of these features, you can get them back by
adding the <a href="https://github.com/rails/activesupport-json_encoder"><code>activesupport-json_encoder</code></a>
gem to your <code>Gemfile</code>.</p><h5 id="json-representation-of-time-objects"><a class="anchorlink" href="#json-representation-of-time-objects">7.7.4 JSON representation of Time objects</a></h5><p><code>#as_json</code> for objects with time component (<code>Time</code>, <code>DateTime</code>, <code>ActiveSupport::TimeWithZone</code>)
now returns millisecond precision by default. If you need to keep old behavior with no millisecond
precision, set the following in an initializer:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
ActiveSupport::JSON::Encoding.time_precision = 0
</pre>
</div>
<h4 id="usage-of-return-within-inline-callback-blocks"><a class="anchorlink" href="#usage-of-return-within-inline-callback-blocks">7.8 Usage of <code>return</code> within inline callback blocks</a></h4><p>Previously, Rails allowed inline callback blocks to use <code>return</code> this way:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class ReadOnlyModel < ActiveRecord::Base
before_save { return false } # BAD
end
</pre>
</div>
<p>This behavior was never intentionally supported. Due to a change in the internals
of <code>ActiveSupport::Callbacks</code>, this is no longer allowed in Rails 4.1. Using a
<code>return</code> statement in an inline callback block causes a <code>LocalJumpError</code> to
be raised when the callback is executed.</p><p>Inline callback blocks using <code>return</code> can be refactored to evaluate to the
returned value:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class ReadOnlyModel < ActiveRecord::Base
before_save { false } # GOOD
end
</pre>
</div>
<p>Alternatively, if <code>return</code> is preferred it is recommended to explicitly define
a method:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class ReadOnlyModel < ActiveRecord::Base
before_save :before_save_callback # GOOD
private
def before_save_callback
return false
end
end
</pre>
</div>
<p>This change applies to most places in Rails where callbacks are used, including
Active Record and Active Model callbacks, as well as filters in Action
Controller (e.g. <code>before_action</code>).</p><p>See <a href="https://github.com/rails/rails/pull/13271">this pull request</a> for more
details.</p><h4 id="methods-defined-in-active-record-fixtures"><a class="anchorlink" href="#methods-defined-in-active-record-fixtures">7.9 Methods defined in Active Record fixtures</a></h4><p>Rails 4.1 evaluates each fixture's ERB in a separate context, so helper methods
defined in a fixture will not be available in other fixtures.</p><p>Helper methods that are used in multiple fixtures should be defined on modules
included in the newly introduced <code>ActiveRecord::FixtureSet.context_class</code>, in
<code>test_helper.rb</code>.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
module FixtureFileHelpers
def file_sha(path)
Digest::SHA2.hexdigest(File.read(Rails.root.join('test/fixtures', path)))
end
end
ActiveRecord::FixtureSet.context_class.include FixtureFileHelpers
</pre>
</div>
<h4 id="i18n-enforcing-available-locales"><a class="anchorlink" href="#i18n-enforcing-available-locales">7.10 I18n enforcing available locales</a></h4><p>Rails 4.1 now defaults the I18n option <code>enforce_available_locales</code> to <code>true</code>. This
means that it will make sure that all locales passed to it must be declared in
the <code>available_locales</code> list.</p><p>To disable it (and allow I18n to accept <em>any</em> locale option) add the following
configuration to your application:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.i18n.enforce_available_locales = false
</pre>
</div>
<p>Note that this option was added as a security measure, to ensure user input
cannot be used as locale information unless it is previously known. Therefore,
it's recommended not to disable this option unless you have a strong reason for
doing so.</p><h4 id="mutator-methods-called-on-relation"><a class="anchorlink" href="#mutator-methods-called-on-relation">7.11 Mutator methods called on Relation</a></h4><p><code>Relation</code> no longer has mutator methods like <code>#map!</code> and <code>#delete_if</code>. Convert
to an <code>Array</code> by calling <code>#to_a</code> before using these methods.</p><p>It intends to prevent odd bugs and confusion in code that call mutator
methods directly on the <code>Relation</code>.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Instead of this
Author.where(name: 'Hank Moody').compact!
# Now you have to do this
authors = Author.where(name: 'Hank Moody').to_a
authors.compact!
</pre>
</div>
<h4 id="changes-on-default-scopes"><a class="anchorlink" href="#changes-on-default-scopes">7.12 Changes on Default Scopes</a></h4><p>Default scopes are no longer overridden by chained conditions.</p><p>In previous versions when you defined a <code>default_scope</code> in a model
it was overridden by chained conditions in the same field. Now it
is merged like any other scope.</p><p>Before:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class User < ActiveRecord::Base
default_scope { where state: 'pending' }
scope :active, -> { where state: 'active' }
scope :inactive, -> { where state: 'inactive' }
end
User.all
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending'
User.active
# SELECT "users".* FROM "users" WHERE "users"."state" = 'active'
User.where(state: 'inactive')
# SELECT "users".* FROM "users" WHERE "users"."state" = 'inactive'
</pre>
</div>
<p>After:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class User < ActiveRecord::Base
default_scope { where state: 'pending' }
scope :active, -> { where state: 'active' }
scope :inactive, -> { where state: 'inactive' }
end
User.all
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending'
User.active
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending' AND "users"."state" = 'active'
User.where(state: 'inactive')
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending' AND "users"."state" = 'inactive'
</pre>
</div>
<p>To get the previous behavior it is needed to explicitly remove the
<code>default_scope</code> condition using <code>unscoped</code>, <code>unscope</code>, <code>rewhere</code> or
<code>except</code>.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class User < ActiveRecord::Base
default_scope { where state: 'pending' }
scope :active, -> { unscope(where: :state).where(state: 'active') }
scope :inactive, -> { rewhere state: 'inactive' }
end
User.all
# SELECT "users".* FROM "users" WHERE "users"."state" = 'pending'
User.active
# SELECT "users".* FROM "users" WHERE "users"."state" = 'active'
User.inactive
# SELECT "users".* FROM "users" WHERE "users"."state" = 'inactive'
</pre>
</div>
<h4 id="rendering-content-from-string"><a class="anchorlink" href="#rendering-content-from-string">7.13 Rendering content from string</a></h4><p>Rails 4.1 introduces <code>:plain</code>, <code>:html</code>, and <code>:body</code> options to <code>render</code>. Those
options are now the preferred way to render string-based content, as it allows