Ruby on Rails
Notes for Professionals
®
Rails
Notes for Professionals
200+ pages
of professional hints and tricks
Disclaimer
GoalKicker.com This is an unocial free book created for educational purposes and is
not aliated with ocial Ruby® on Rails group(s) or company(s).
Free Programming Books All trademarks and registered trademarks are
the property of their respective owners
,Contents
About ................................................................................................................................................................................... 1
Chapter 1: Getting started with Ruby on Rails .............................................................................................. 2
Section 1.1: Creating a Ruby on Rails Application ...................................................................................................... 2
Section 1.2: Create a new Rails app with your choice of database and including the RSpec Testing Tool
.................................................................................................................................................................................. 4
Section 1.3: Generating A Controller ............................................................................................................................ 4
Section 1.4: Installing Rails ............................................................................................................................................ 5
Section 1.5: Create a new Rails app with a non-standard database adapter ........................................................ 7
Section 1.6: Creating Rails APIs in JSON ...................................................................................................................... 7
Section 1.7: Generate a Resource with Scaolds ....................................................................................................... 8
Chapter 2: Routing ..................................................................................................................................................... 10
Section 2.1: Resource Routing (Basic) ....................................................................................................................... 10
Section 2.2: Constraints .............................................................................................................................................. 11
Section 2.3: Scoping routes ........................................................................................................................................ 13
Section 2.4: Concerns .................................................................................................................................................. 15
Section 2.5: Root route ............................................................................................................................................... 16
Section 2.6: Split routes into multiple files ................................................................................................................ 16
Section 2.7: Additional RESTful actions ..................................................................................................................... 17
Section 2.8: Member and Collection Routes ............................................................................................................. 17
Section 2.9: Mount another application .................................................................................................................... 18
Section 2.10: Nested Routes ....................................................................................................................................... 18
Section 2.11: Redirection .............................................................................................................................................. 19
Section 2.12: Redirects and Wildcard Routes ........................................................................................................... 19
Section 2.13: Scope available locales ........................................................................................................................ 19
Section 2.14: URL params with a period ................................................................................................................... 20
Chapter 3: ActiveRecord ......................................................................................................................................... 21
Section 3.1: Creating a Model via generator ............................................................................................................. 21
Section 3.2: Introduction to Callbacks ....................................................................................................................... 21
Section 3.3: Creating a Model manually ................................................................................................................... 22
Section 3.4: Manually Testing Your Models .............................................................................................................. 23
Section 3.5: Creating A Migration .............................................................................................................................. 23
Section 3.6: Create a Join Table using Migrations ................................................................................................... 24
Section 3.7: Using a model instance to update a row ............................................................................................. 25
Chapter 4: Views ......................................................................................................................................................... 26
Section 4.1: Structure ................................................................................................................................................... 26
Section 4.2: Partials ..................................................................................................................................................... 26
Section 4.3: AssetTagHelper ...................................................................................................................................... 27
Section 4.4: Replace HTML code in Views ................................................................................................................ 28
Section 4.5: HAML - an alternative way to use in your views ................................................................................. 29
Chapter 5: ActiveRecord Migrations ................................................................................................................. 31
Section 5.1: Adding multiple columns to a table ....................................................................................................... 31
Section 5.2: Add a reference column to a table ...................................................................................................... 31
Section 5.3: Rollback migrations ................................................................................................................................ 32
Section 5.4: Add a new column with an index .......................................................................................................... 32
Section 5.5: Run specific migration ........................................................................................................................... 33
Section 5.6: Redo migrations ..................................................................................................................................... 33
Section 5.7: Add a new column to a table ................................................................................................................ 33
, Section 5.8: Remove an existing column from a table ............................................................................................ 34
Section 5.9: Add column with default value ............................................................................................................. 34
Section 5.10: Running migrations in dierent environments .................................................................................. 35
Section 5.11: Create a new table ................................................................................................................................. 35
Section 5.12: Running migrations ............................................................................................................................... 35
Section 5.13: Change an existing column’s type ....................................................................................................... 35
Section 5.14: Create a hstore column ........................................................................................................................ 36
Section 5.15: Create a join table ................................................................................................................................. 36
Section 5.16: Add a self reference .............................................................................................................................. 37
Section 5.17: Create an array column ....................................................................................................................... 37
Section 5.18: Add an unique column to a table ........................................................................................................ 37
Section 5.19: Checking migration status ................................................................................................................... 38
Section 5.20: Changing Tables .................................................................................................................................. 38
Section 5.21: Adding a NOT NULL constraint to existing data ................................................................................ 38
Section 5.22: Forbid null values ................................................................................................................................. 39
Chapter 6: Rails Best Practices ............................................................................................................................ 40
Section 6.1: Fat Model, Skinny Controller .................................................................................................................. 40
Section 6.2: Domain Objects (No More Fat Models) ............................................................................................... 40
Section 6.3: Beware of default_scope ...................................................................................................................... 42
Section 6.4: Convention Over Configuration ............................................................................................................ 44
Section 6.5: Don't Repeat Yourself (DRY) ................................................................................................................. 45
Section 6.6: You Ain’t Gonna Need it (YAGNI) ........................................................................................................... 45
Chapter 7: Naming Conventions ......................................................................................................................... 47
Section 7.1: Controllers ................................................................................................................................................ 47
Section 7.2: Models ...................................................................................................................................................... 47
Section 7.3: Filenames and autoloading ................................................................................................................... 47
Section 7.4: Views and Layouts ................................................................................................................................. 48
Section 7.5: Models class from Controller name ...................................................................................................... 48
Chapter 8: ActionCable ............................................................................................................................................ 49
Section 8.1: User Authentication ................................................................................................................................. 49
Section 8.2: [Basic] Server Side ................................................................................................................................. 49
Section 8.3: [Basic] Client Side (Coeescript) .......................................................................................................... 49
Chapter 9: ActiveModel ............................................................................................................................................ 51
Section 9.1: Using ActiveModel::Validations .............................................................................................................. 51
Chapter 10: User Authentication in Rails ........................................................................................................ 52
Section 10.1: Authentication using Devise .................................................................................................................. 52
Section 10.2: Devise Controller Filters & Helpers ...................................................................................................... 52
Section 10.3: Omniauth ................................................................................................................................................ 53
Section 10.4: has_secure_password ......................................................................................................................... 53
Section 10.5: has_secure_token ................................................................................................................................ 53
Chapter 11: ActiveRecord Associations ............................................................................................................ 55
Section 11.1: Polymorphic association ........................................................................................................................ 55
Section 11.2: Self-Referential Association .................................................................................................................. 55
Section 11.3: belongs_to .............................................................................................................................................. 56
Section 11.4: has_one .................................................................................................................................................. 56
Section 11.5: has_many ............................................................................................................................................... 56
Section 11.6: The has_many :through association ................................................................................................... 57
Section 11.7: The has_one :through association ....................................................................................................... 57
Section 11.8: The has_and_belongs_to_many association ................................................................................... 58
, Chapter 12: ActiveRecord Validations .............................................................................................................. 59
Section 12.1: Validating length of an attribute .......................................................................................................... 59
Section 12.2: Validates format of an attribute .......................................................................................................... 59
Section 12.3: Validating presence of an attribute .................................................................................................... 59
Section 12.4: Custom validations ................................................................................................................................ 60
Section 12.5: Validates inclusion of an attribute ....................................................................................................... 60
Section 12.6: Grouping validation ............................................................................................................................... 61
Section 12.7: Validating numericality of an attribute ............................................................................................... 61
Section 12.8: Validate uniqueness of an attribute .................................................................................................... 62
Section 12.9: Skipping Validations .............................................................................................................................. 62
Section 12.10: Confirmation of attribute .................................................................................................................... 62
Section 12.11: Using :on option .................................................................................................................................... 63
Section 12.12: Conditional validation .......................................................................................................................... 63
Chapter 13: ActiveRecord Query Interface .................................................................................................... 64
Section 13.1: .where ...................................................................................................................................................... 64
Section 13.2: .where with an array ............................................................................................................................. 64
Section 13.3: Scopes .................................................................................................................................................... 65
Section 13.4: Get first and last record ........................................................................................................................ 66
Section 13.5: Ordering ................................................................................................................................................. 67
Section 13.6: where.not ................................................................................................................................................ 67
Section 13.7: Includes ................................................................................................................................................... 68
Section 13.8: Joins ........................................................................................................................................................ 68
Section 13.9: Limit and Oset ..................................................................................................................................... 69
Section 13.10: .find_by ................................................................................................................................................. 69
Section 13.11: .delete_all .............................................................................................................................................. 69
Section 13.12: ActiveRecord case insensitive search ................................................................................................ 69
Section 13.13: .group and .count ................................................................................................................................. 70
Section 13.14: .distinct (or .uniq) .................................................................................................................................. 70
Chapter 14: ActionMailer ......................................................................................................................................... 71
Section 14.1: Basic Mailer ............................................................................................................................................. 71
Section 14.2: Generating a new mailer ...................................................................................................................... 72
Section 14.3: ActionMailer Interceptor ....................................................................................................................... 72
Section 14.4: Adding Attachments ............................................................................................................................. 72
Section 14.5: ActionMailer Callbacks .......................................................................................................................... 73
Section 14.6: Generate a Scheduled Newsletter ....................................................................................................... 73
Chapter 15: Rails generate commands ........................................................................................................... 79
Section 15.1: Rails Generate Controller ...................................................................................................................... 79
Section 15.2: Rails Generate Migration ...................................................................................................................... 80
Section 15.3: Rails Generate Scaold ........................................................................................................................ 80
Section 15.4: Rails Generate Model ............................................................................................................................ 81
Chapter 16: Configuration ...................................................................................................................................... 82
Section 16.1: Custom configuration ............................................................................................................................ 82
Chapter 17: I18n - Internationalization ............................................................................................................. 84
Section 17.1: I18n with arguments ............................................................................................................................... 84
Section 17.2: Translating ActiveRecord model attributes ........................................................................................ 84
Section 17.3: Get locale from HTTP request .............................................................................................................. 86
Section 17.4: Pluralization ............................................................................................................................................ 87
Section 17.5: Set locale through requests ................................................................................................................. 87
Section 17.6: Use I18n with HTML Tags and Symbols .............................................................................................. 89
Section 17.7: Use I18n in views .................................................................................................................................... 89