Online Sample Chapter
Advanced Active Record in Rails 4
Excerpt
Foreword to The Rails 4 Way
Sample Pages
Download the sample pages (includes Chapter 9 and Index)
Table of Contents
Foreword by Steve Klabnik xxxix
Foreword to the Previous Edition by David Heinemeier Hansson xli
Foreword to the Previous Edition by Yehuda Katz xliii
Introduction xlv
Acknowledgments li
About the Authors liii
Chapter 1: Rails Environments and Configuration 1
1.1 Bundler 2
1.2 Startup and Application Settings 9
1.3 Development Mode 18
1.4 Test Mode 22
1.5 Production Mode 23
1.6 Configuring a Database 26
1.7 Configuring Application Secrets 27
1.8 Logging 29
1.9 Conclusion 35
Chapter 2: Routing 37
2.1 The Two Purposes of Routing 38
2.2 The routes.rb File 39
2.3 Route Globbing 51
2.4 Named Routes 53
2.5 Scoping Routing Rules 57
2.6 Listing Routes 60
2.7 Conclusion 61
Chapter 3: REST, Resources, and Rails 63
3.1 REST in a Rather Small Nutshell 63
3.2 Resources and Representations 64
3.3 REST in Rails 65
3.4 Routing and CRUD 66
3.5 The Standard RESTful Controller Actions 69
3.6 Singular Resource Routes 73
3.7 Nested Resources 74
3.8 Routing Concerns 78
3.9 RESTful Route Customizations 79
3.10 Controller-Only Resources 83
3.11 Different Representations of Resources 86
3.12 The RESTful Rails Action Set 88
3.13 Conclusion 92
Chapter 4: Working with Controllers 95
4.1 Rack 96
4.2 Action Dispatch: Where It All Begins 99
4.3 Render unto View… 102
4.4 Additional Layout Options 111
4.5 Redirecting 111
4.6 Controller/View Communication 115
4.7 Action Callbacks 116
4.8 Streaming 121
4.9 Variants 126
4.10 Conclusion 127
Chapter 5: Working with Active Record 129
5.1 The Basics 130
5.2 Macro-Style Methods 131
5.3 Defining Attributes 133
5.4 CRUD: Create, Read, Update, and Delete 138
5.5 Database Locking 151
5.6 Where Clauses 155
5.7 Connections to Multiple Databases in Different Models 165
5.8 Using the Database Connection Directly 167
5.9 Other Configuration Options 171
5.10 Conclusion 171
Chapter 6: Active Record Migrations 173
6.1 Creating Migrations 173
6.2 Data Migration 187
6.3 schema.rb 189
6.4 Database Seeding 190
6.5 Database-Related Rake Tasks 191
6.6 Conclusion 194
Chapter 7: Active Record Associations 195
7.1 The Association Hierarchy 195
7.2 One-to-Many Relationships 196
7.3 The belongs_to Association 205
7.4 The has_many Association 214
7.5 Many-to-Many Relationships 222
7.6 One-to-One Relationships 233
7.7 Working with Unsaved Objects and Associations 236
7.8 Association Extensions 238
7.9 The CollectionProxy Class 239
7.10 Conclusion 240
Chapter 8: Validations 241
8.1 Finding Errors 241
8.2 The Simple Declarative Validations 242
8.3 Common Validation Options 253
8.4 Conditional Validation 255
8.5 Short-Form Validation 256
8.6 Custom Validation Techniques 258
8.7 Skipping Validations 260
8.8 Working with the Errors Hash 261
8.9 Testing Validations with Shoulda 262
8.10 Conclusion 262
Chapter 9: Advanced Active Record 263
9.1 Scopes 263
9.2 Callbacks 268
9.3 Calculation Methods 278
9.4 Single-Table Inheritance (STI) 280
9.5 Abstract Base Model Classes 286
9.6 Polymorphic has_many Relationships 287
9.7 Enums 290
9.8 Foreign-Key Constraints 292
9.9 Modules for Reusing Common Behavior 292
9.10 Modifying Active Record Classes at Runtime 297
9.11 Using Value Objects 299
9.12 Nonpersisted Models 302
9.13 PostgreSQL Enhancements 304
9.14 Conclusion 311
Chapter 10: Action View 313
10.1 Layouts and Templates 314
10.2 Partials 322
10.3 Conclusion 329
Chapter 11: All about Helpers 331
11.1 ActiveModelHelper 331
11.2 AssetTagHelper 338
11.3 AtomFeedHelper 346
11.4 CacheHelper 348
11.5 CaptureHelper 348
11.6 CsrfHelper 349
11.7 DateHelper 349
11.8 DebugHelper 356
11.9 FormHelper 357
11.10 FormOptionsHelper 371
11.11 FormTagHelper 379
11.12 JavaScriptHelper 385
11.13 NumberHelper 385
11.14 OutputSafetyHelper 390
11.15 RecordTagHelper 390
11.16 RenderingHelper 391
11.17 SanitizeHelper 391
11.18 TagHelper 393
11.19 TextHelper 395
11.20 TranslationHelper and the I18n API 399
11.21 UrlHelper 418
11.22 Writing Your Own View Helpers 422
11.23 Wrapping and Generalizing Partials 425
11.24 Conclusion 431
Chapter 12: Haml 433
12.1 Getting Started 434
12.2 The Basics 434
12.3 Doctype 440
12.4 Comments 440
12.5 Evaluating Ruby Code 441
12.6 Helpers 443
12.7 Filters 444
12.8 Haml and Content 445
12.9 Configuration Options 446
12.10 Conclusion 448
Chapter 13: Session Management 449
13.1 What to Store in the Session 450
13.2 Session Options 451
13.3 Storage Mechanisms 451
13.4 Cookies 455
13.5 Conclusion 457
Chapter 14: Authentication and Authorization 459
14.1 Devise 459
14.2 has_secure_password 466
14.3 Pundit 470
14.4 Conclusion 476
Chapter 15: Security 477
15.1 Password Management 477
15.2 Log Masking 479
15.3 SSL (Secure Sockets Layer) 480
15.4 Model Mass-Assignment Attributes Protection 481
15.5 SQL Injection 483
15.6 Cross-Site Scripting (XSS) 484
15.7 XSRF (Cross-Site Request Forgery) 487
15.8 Session Fixation Attacks 490
15.9 Keeping Secrets 491
15.10 Conclusion 492
Chapter 16: Action Mailer 493
16.1 Setup 493
16.2 Mailer Models 494
16.3 Receiving Emails 500
16.4 Server Configuration 502
16.5 Testing Email Content 502
16.6 Previews 503
16.7 Conclusion 504
Chapter 17: Caching and Performance 505
17.1 View Caching 505
17.2 Data Caching 521
17.3 Control of Web Caching 523
17.4 ETags 524
17.5 Conclusion 526
Chapter 18: Background Processing 527
18.1 Delayed Job 528
18.2 Sidekiq 531
18.3 Resque 537
18.4 Rails Runner 541
18.5 Conclusion 543
Chapter 19: Ajax on Rails 545
19.1 Unobtrusive JavaScript 547
19.2 Turbolinks 551
19.3 Ajax and JSON 553
19.4 Ajax and HTML 555
19.5 Ajax and JavaScript 557
19.6 Conclusion 558
Chapter 20: Asset Pipeline 559
20.1 Asset Pipeline 560
20.2 Wish List 560
20.3 The Big Picture 561
20.4 Organization: Where Does Everything Go? 561
20.5 Manifest Files 561
20.6 Custom Format Handlers 567
20.7 Postprocessing 568
20.8 Helpers 569
20.9 Fingerprinting 571
20.10 Serving the Files 572
20.11 Rake Tasks 573
20.12 Conclusion 574
Chapter 21: RSpec 575
21.1 Introduction 575
21.2 Basic Syntax and API 578
21.3 Matchers 587
21.4 Custom Expectation Matchers 588
21.5 Shared Behaviors 591
21.6 Shared Context 592
21.7 RSpec’s Mocks and Stubs 592
21.8 Running Specs 595
21.9 RSpec Rails Gem 596
21.10 RSpec Tools 609
21.11 Conclusion 610
Chapter 22: XML 611
22.1 The to_xml Method 611
22.2 The XML Builder 620
22.3 Parsing XML 622
22.4 Conclusion 624
Appendix A: Active Model API Reference 625
A.1 AttributeMethods 625
A.2 Callbacks 627
A.3 Conversion 629
A.4 Dirty 629
A.5 Errors 631
A.6 ForbiddenAttributesError 635
A.7 Lint::Tests 635
A.8 Model 635
A.9 Name 636
A.10 Naming 638
A.11 SecurePassword 638
A.12 Serialization 638
A.13 Serializers::JSON 639
A.14 Serializers::Xml 639
A.15 Translation 640
A.16 Validations 641
A.17 Validator 648
Appendix B: Active Support API Reference 651
B.1 Array 651
B.2 ActiveSupport::BacktraceCleaner 657
B.3 Benchmark 658
B.4 ActiveSupport::Benchmarkable 658
B.5 BigDecimal 659
B.6 ActiveSupport::Cache::Store 660
B.7 ActiveSupport::CachingKeyGenerator 665
B.8 ActiveSupport::Callbacks 665
B.9 Class 668
B.10 ActiveSupport::Concern 671
B.11 ActiveSupport::Concurrency 672
B.12 ActiveSupport::Configurable 673
B.13 Date 673
B.14 DateTime 682
B.15 ActiveSupport::Dependencies 687
B.16 ActiveSupport::Deprecation 693
B.17 ActiveSupport::DescendantsTracker 694
B.18 ActiveSupport::Duration 695
B.19 Enumerable 696
B.20 ERB::Util 697
B.21 FalseClass 698
B.22 File 698
B.23 Hash 699
B.24 ActiveSupport::Gzip 704
B.25 ActiveSupport::HashWithIndifferentAccess 705
B.26 ActiveSupport::Inflector::Inflections 705
B.27 Integer 711
B.28 ActiveSupport::JSON 712
B.29 Kernel 712
B.30 ActiveSupport::KeyGenerator 714
B.31 ActiveSupport::Logger 714
B.32 ActiveSupport::MessageEncryptor 715
B.33 ActiveSupport::MessageVerifier 715
B.34 Module 716
B.35 ActiveSupport::Multibyte::Chars 724
B.36 NilClass 729
B.37 ActiveSupport::Notifications 729
B.38 Object 738
B.39 ActiveSupport::OrderedHash 743
B.40 ActiveSupport::OrderedOptions 743
B.41 ActiveSupport::PerThreadRegistry 744
B.42 ActiveSupport::ProxyObject 744
B.43 ActiveSupport::Railtie 745
B.44 Range 746
B.45 Regexp 747
B.46 ActiveSupport::Rescuable 748
B.47 String 748
B.48 ActiveSupport::StringInquirer 758
B.49 Struct 758
B.50 ActiveSupport::Subscriber 758
B.51 Symbol 759
B.52 ActiveSupport::TaggedLogging 759
B.53 ActiveSupport::TestCase 759
B.54 ActiveSupport::Testing::Assertions 761
B.55 Thread 762
B.56 Time 763
B.57 ActiveSupport::TimeWithZone 773
B.58 ActiveSupport::TimeZone 774
B.59 TrueClass 778
B.60 ActiveSupport::XmlMini 778
Appendix C: Rails Essentials 781
C.1 Environmental Concerns 781
C.2 Essential Gems 782
C.3 Ruby Toolbox 789
C.4 Screencasts 789
Index 791
Praise For Rails 4 Way, The, 3rd Edition
"For intermediates and above, I strongly recommend adding this title to your technical bookshelf. There is simply no other Rails title on the market at this time that offers the technical depth of the framework than The Rails™ 3 Way."
—Mike Riley, Dr. Dobb's Journal
"I highly suggest you get this book. Software moves fast, especially the Rails API, but I feel this book has many core API and development concepts that will be useful for a while to come."
—Matt Polito, software engineer and member of Chicago Ruby User Group
"This book should live on your desktop if you’re a Rails developer. It’s nearly perfect in my opinion."
—Luca Pette, developer
"The Rails™ 3 Way is likely to take you from being a haphazard poke-a-stick-at-it programmer to a deliberate, skillful, productive, and confident RoR developer."
—Katrina Owen, JavaRanch
'I can positively say that it's the single best Rails book ever published to date. By a long shot."
—Antonio Cangiano, software engineer and technical evangelist at IBM
"This book is a great crash course in Ruby on Rails! It doesn’t just document the features of Rails, it filters everything through the lens of an experienced Rails developer—so you come out a pro on the other side."
—Dirk Elmendorf, cofounder of Rackspace Inc. and Rails developer
"The key to The Rails Way is in the title. It literally covers the 'way' to do almost everything with Rails. Writing a truly exhaustive reference to the most popular web application framework used by thousands of developers is no mean feat. A thankful community of developers that has struggled to rely on scant documentation will embrace The Rails Way with open arms. A tour de force!"
—Peter Cooper, editor, Ruby Inside: The Ruby Blog
"In the past year, dozens of Rails books have been rushed to publication. A handful are good. Most regurgitate rudimentary information easily found on the Web. Only this book provides both the broad and deep technicalities of Rails. Nascent and expert developers, I recommend you follow The Rails Way."
—Martin Streicher, chief technology officer, McClatchy Interactive, former editor in chief of Linux Magazine
"Hal Fulton's The Ruby Way has always been by my side as a reference while programming Ruby. Many times I had wished there was a book that had the same depth and attention to detail, only focused on the Rails framework. That book is now here and hasn't left my desk for the past month."
—Nate Klaiber, Ruby programmer
"I knew soon after becoming involved with Rails that I had found something great. Now, with Obie's book, I have been able to step into Ruby on Rails development coming from .NET and be productive right away. The applications I have created I believe to be a much better quality due to the techniques I learned using Obie's knowledge."
—Robert Bazinet, InfoQ.com, .NET, and Ruby community editor and founding member of the Hartford Ruby Brigade
"Extremely well written; it's a resource that every Rails programmer should have. Yes, it's that good."
—Reuven Lerner, Linux Journal columnist