SlideShare a Scribd company logo
Ruby on Rails
                 Best Practices
                                                                             Vladimir Tkach


                            Wondering why Ruby is so popular?
                            Its fans call it a beautiful, artful language.
                            And yet, they say it’s handy and practical.


http://www.ruby-lang.org/




http://rubyonrails.org
How to?




© SAP 2007 / Page 2
Rails Best Practices




© SAP 2007 / Page 3
Concept: What’s good code?




© SAP 2007 / Page 4
Move finder to named_scope / scope




© SAP 2007 / Page 5
Use model association




© SAP 2007 / Page 6
Use scope access




© SAP 2007 / Page 7
Add model virtual attribute




© SAP 2007 / Page 8
Use model callback




© SAP 2007 / Page 9
Use Model Callback




                                       %>




© SAP 2007 / Page 10
Replace Complex Creation
                         with Factory Method




© SAP 2007 / Page 11
Replace Complex Creation
                         with Factory Method




© SAP 2007 / Page 12
Move Model Logic into the Model




© SAP 2007 / Page 13
model.collection_model_ids
                            (many-to-many)




© SAP 2007 / Page 14
model.collection_model_ids
                            (many-to-many)




© SAP 2007 / Page 15
model.collection_model_ids
                            (many-to-many)




© SAP 2007 / Page 16
Nested Model Forms (one-to-one)




© SAP 2007 / Page 17
Nested Model Forms (one-to-one)




© SAP 2007 / Page 18
Nested Model Forms (one-to-many)




© SAP 2007 / Page 19
Keep Finders on Their Own Model




© SAP 2007 / Page 20
Keep Finders on Their Own Model




© SAP 2007 / Page 21
Love named_scope




© SAP 2007 / Page 22
Love named_scope




© SAP 2007 / Page 23
the Law of Demeter




© SAP 2007 / Page 24
DRY: Metaprogramming




© SAP 2007 / Page 25
DRY: Metaprogramming




© SAP 2007 / Page 26
Extract into Module




© SAP 2007 / Page 27
Extract to composed class




© SAP 2007 / Page 28
Extract to composed class




© SAP 2007 / Page 29
Use Observer




© SAP 2007 / Page 30
Use Observer




© SAP 2007 / Page 31
Move code into controller




© SAP 2007 / Page 32
Move code into model




© SAP 2007 / Page 33
Move code into helper




© SAP 2007 / Page 34
Replace instance variable
                          with local variable




© SAP 2007 / Page 35
Thank you!
  §  http://www.ruby-lang.org/en/
  §  http://www.ruby-lang.org/en/documentation/
  §  http://rubyonrails.org/
  §  http://guides.rubyonrails.org/v2.3.8/index.html


  §     http://teamco-anthill.blogspot.com




© SAP 2007 / Page 36

More Related Content

PDF
Ruby On Rails coding conventions, standards and best practices
PDF
Rails Best Practices
PDF
Rails Best Practices
PDF
RSpec 2 Best practices
PDF
Axel uhl sap@md-day2011
KEY
AngularJS for designers and developers
KEY
Refactoring at Large
PDF
Enterprise PHP: mappers, models and services
Ruby On Rails coding conventions, standards and best practices
Rails Best Practices
Rails Best Practices
RSpec 2 Best practices
Axel uhl sap@md-day2011
AngularJS for designers and developers
Refactoring at Large
Enterprise PHP: mappers, models and services

Similar to Ruby on rails. Best practices (20)

PDF
td_mxc_rubyrails_shin
PDF
td_mxc_rubyrails_shin
PDF
Underlaying Technology of Modern O/R Mapper
PDF
Abap objects
PDF
Abap objects
PPTX
Building Maintainable PHP Applications.pptx
PPTX
Data Access Tech Ed India
PDF
Rails MVC by Sergiy Koshovyi
PPTX
Solid Software Design Principles
DOC
10264 developing web applications with microsoft visual studio 2010
PPTX
XEO Framework - TDose 2011
PDF
Asp.Net Mvc Dev Days09
PDF
Carlosbrando Rubyonrails21 En
PDF
Building Large Web Applications That Are Easy to Maintain
PDF
Wakanda model-driven @ ICSSEA12
PDF
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...
PDF
LeverX - A Comprehensive Guide to SAP PLM 7.01
PDF
Ruby on Rails 2.1 What's New
KEY
25 Real Life Tips In Ruby on Rails Development
PDF
Chris Madrid Service Virtualization
td_mxc_rubyrails_shin
td_mxc_rubyrails_shin
Underlaying Technology of Modern O/R Mapper
Abap objects
Abap objects
Building Maintainable PHP Applications.pptx
Data Access Tech Ed India
Rails MVC by Sergiy Koshovyi
Solid Software Design Principles
10264 developing web applications with microsoft visual studio 2010
XEO Framework - TDose 2011
Asp.Net Mvc Dev Days09
Carlosbrando Rubyonrails21 En
Building Large Web Applications That Are Easy to Maintain
Wakanda model-driven @ ICSSEA12
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...
LeverX - A Comprehensive Guide to SAP PLM 7.01
Ruby on Rails 2.1 What's New
25 Real Life Tips In Ruby on Rails Development
Chris Madrid Service Virtualization
Ad

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Electronic commerce courselecture one. Pdf
PDF
Modernizing your data center with Dell and AMD
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
Teaching material agriculture food technology
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
Electronic commerce courselecture one. Pdf
Modernizing your data center with Dell and AMD
20250228 LYD VKU AI Blended-Learning.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Per capita expenditure prediction using model stacking based on satellite ima...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
Spectral efficient network and resource selection model in 5G networks
Building Integrated photovoltaic BIPV_UPV.pdf
Teaching material agriculture food technology
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Unlocking AI with Model Context Protocol (MCP)
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Understanding_Digital_Forensics_Presentation.pptx
Ad

Ruby on rails. Best practices