SlideShare a Scribd company logo
RAILS ORM
             de-mystifying ActiveRecord




Thursday, December 8, 11
NoteToSelf: Know your audience

                    •      Who has used an ORM?

                    •      Daily:

                           •   Who uses Relational Databases?

                           •   Who uses Java,Erlang,ObjectiveC?

                           •   Who uses PHP, Python?

                           •   Who uses Javascript?

                           •   Who uses [Perl, Smalltalk, Lisp, Ruby,]




Thursday, December 8, 11
NTS: Address a Need
 What I want =>




                    • How can I do my job smoother, faster, and
                           to a higher standard?

   What I need first =>




                    • Where can I can get a ‘run-time’ model of
                           rails?




Thursday, December 8, 11
NTS: Fulfill need

                    • What does Rails do for you?
                    • What do you need to do for Rails?   Rails is Ruby,


                                                          Ruby is Awesome,


                                                          Transitive Relation ,


                                                          Rails is Awesome.




Thursday, December 8, 11
What you are about to see

                    • Papa Bears Honey Sharing Co-Operative




Thursday, December 8, 11
Papa Bear Honey Share




Thursday, December 8, 11
What you are about to see

                    • (2) one-to-many relationships




Thursday, December 8, 11
Internal Data Model




Thursday, December 8, 11
What you are about to see

                    • External Resource Scheme




Thursday, December 8, 11
External REST Resource API




Thursday, December 8, 11
git@github.com:blazingcloud/papabear.git




Thursday, December 8, 11
What you are about to see


                    • database model implies methods




Thursday, December 8, 11
rake db:reset




Thursday, December 8, 11
rails db




Thursday, December 8, 11
PRAGMA table_info(hives);




Thursday, December 8, 11
module ActiveRecord
                             module ConnectionAdapters
                               class SQLiteAdapter < AbstractAdapter


def table_structure(table_name)
  structure = exec_query("PRAGMA
table_info(#{quote_table_name(table_name)})",
'SCHEMA').to_hash
  raise(ActiveRecord::StatementInvalid, "Could not
find table '#{table_name}'") if structure.empty?
  structure
end
                             https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb




Thursday, December 8, 11
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_methods.rb




                                      for all table attributes
                                         define a method
                                            to change attribute
                                            to read    attribute




                                                         story!




Thursday, December 8, 11
What you are about to see
                    • Inserting a bunch of pre declared data via
                           External API
                    • Hand waving




Thursday, December 8, 11
rails server &




Thursday, December 8, 11
cat curl.txt
                           sh curl.txt




Thursday, December 8, 11
http://localhost:3000/




Thursday, December 8, 11
What you are about to see

                    • ‘has_many’ declaring an association to
                           ‘honeys’




Thursday, December 8, 11
app/models/hive.rb



          class Hive < ActiveRecord::Base
            belongs_to :field_of_origin
            has_many :honeys # product
          end




Thursday, December 8, 11
activerecord/lib/active_record/associations.rb




                                module ActiveRecord
                                  module Associations

               def has_many(name, options = {}, &extension)
                Builder::HasMany.build(self, name, options, &extension)
               end




                                 https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations.rb




Thursday, December 8, 11
activerecord/lib/active_record/associations/builder/association.rb


                           module ActiveRecord::Associations::Builder
                             class Association
                            def self.build(model, name, options)
                              new(model, name, options).build
                            end

                            def define_readers
                              name = self.name
                              mixin.redefine_method(name) do |*params|
                                association(name).reader(*params)
                              end
                            end

                            https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/association.rb




Thursday, December 8, 11
What you are about to see
                    • ‘honeys’ reader method
                    • ‘has_many’ builds methods like ‘create!’ on
                           the reader association




Thursday, December 8, 11
app/controllers/honeys_controller.rb



          class HoneysController < ApplicationController
            def create
              current_hive = Hive.find(params[:hive_id])
              if current_hive
                current_hive.honeys.create!(params[:honey])
                render :json => current_hive.to_json(:include => :honeys)
              else
                head :error
              end
            end
          end




Thursday, December 8, 11
activerecord/lib/active_record/associations/collection_association.rb




                           module ActiveRecord
                             module Associations
                               class CollectionAssociation < Association

                             def create!(attributes = {}, options = {}, &block)
                               create_record(attributes, options, true, &block)
                             end




                    https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/collection_association.rb




Thursday, December 8, 11
SEE CURL POST




        curl -d 'honey[name]=Cinnamon%20Manuka%20&honey[volume_in_ml]=2520' http://localhost:3000/hives/1/honeys




Thursday, December 8, 11
What does Rails ORM do for me?


                    • Keeps me focused on describing the
                           domain
                    • Keeps me from re-expresing my data model
                           in query/strings/logic/controllers/libraries
                    • Keeps me from having to write generic
                           code - focus on what is specific.




Thursday, December 8, 11
What do I need to do for Rails ORM?



                    • learn Rails conventions
                    • learn Rails limits
                    • express my design in it’s language



Thursday, December 8, 11
Where can I get a Realtime / Runtime
                           understanding of what Rails is doing?


                    • practice
                    • errors
                    • reflection
                    • rinse & repeat


Thursday, December 8, 11
quest-ions?




Thursday, December 8, 11

More Related Content

What's hot

Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Elena Kolevska
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - IntroductionVagmi Mudumbai
 
Laravel 5 In Depth
Laravel 5 In DepthLaravel 5 In Depth
Laravel 5 In DepthKirk Bushell
 
Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Henry S
 
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...Ryan Weaver
 
REST APIs in Laravel 101
REST APIs in Laravel 101REST APIs in Laravel 101
REST APIs in Laravel 101Samantha Geitz
 
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...Puppet
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on RailsManoj Kumar
 
When To Use Ruby On Rails
When To Use Ruby On RailsWhen To Use Ruby On Rails
When To Use Ruby On Railsdosire
 
Flickr Architecture Presentation
Flickr Architecture PresentationFlickr Architecture Presentation
Flickr Architecture Presentationeraz
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentNicolas Ledez
 
Ruby off Rails (english)
Ruby off Rails (english)Ruby off Rails (english)
Ruby off Rails (english)Stoyan Zhekov
 
Ruby on Rails For .Net Programmers
Ruby on Rails For .Net ProgrammersRuby on Rails For .Net Programmers
Ruby on Rails For .Net Programmersdaveverwer
 
Keeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro frameworkKeeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro frameworkJeremy Kendall
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackIgnacio Martín
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails PresentationJoost Hietbrink
 

What's hot (20)

Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - Introduction
 
Laravel 5 In Depth
Laravel 5 In DepthLaravel 5 In Depth
Laravel 5 In Depth
 
Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Learning to code for startup mvp session 3
Learning to code for startup mvp session 3
 
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
 
REST APIs in Laravel 101
REST APIs in Laravel 101REST APIs in Laravel 101
REST APIs in Laravel 101
 
REST in pieces
REST in piecesREST in pieces
REST in pieces
 
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
When To Use Ruby On Rails
When To Use Ruby On RailsWhen To Use Ruby On Rails
When To Use Ruby On Rails
 
Ruby talk romania
Ruby talk romaniaRuby talk romania
Ruby talk romania
 
Flickr Architecture Presentation
Flickr Architecture PresentationFlickr Architecture Presentation
Flickr Architecture Presentation
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirent
 
Ruby off Rails (english)
Ruby off Rails (english)Ruby off Rails (english)
Ruby off Rails (english)
 
Ruby on Rails For .Net Programmers
Ruby on Rails For .Net ProgrammersRuby on Rails For .Net Programmers
Ruby on Rails For .Net Programmers
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
 
Laravel 101
Laravel 101Laravel 101
Laravel 101
 
Keeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro frameworkKeeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro framework
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and Webpack
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 

Viewers also liked

Rails Class Intro - 1
Rails Class Intro - 1 Rails Class Intro - 1
Rails Class Intro - 1 Blazing Cloud
 
Starting A Business and Finding Your Niche
Starting A Business and Finding Your NicheStarting A Business and Finding Your Niche
Starting A Business and Finding Your NicheCatherine L. Burton
 
Your first rails app - 2
 Your first rails app - 2 Your first rails app - 2
Your first rails app - 2Blazing Cloud
 
Intro to Ruby (and RSpec)
Intro to Ruby (and RSpec)Intro to Ruby (and RSpec)
Intro to Ruby (and RSpec)Blazing Cloud
 

Viewers also liked (7)

Rails Class Intro - 1
Rails Class Intro - 1 Rails Class Intro - 1
Rails Class Intro - 1
 
Starting A Business and Finding Your Niche
Starting A Business and Finding Your NicheStarting A Business and Finding Your Niche
Starting A Business and Finding Your Niche
 
Yangin sunu kopya
Yangin sunu   kopyaYangin sunu   kopya
Yangin sunu kopya
 
Routes Controllers
Routes ControllersRoutes Controllers
Routes Controllers
 
Your first rails app - 2
 Your first rails app - 2 Your first rails app - 2
Your first rails app - 2
 
Enumerables
EnumerablesEnumerables
Enumerables
 
Intro to Ruby (and RSpec)
Intro to Ruby (and RSpec)Intro to Ruby (and RSpec)
Intro to Ruby (and RSpec)
 

Similar to Rails ORM De-mystifying Active Record has_many

Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex Espen Brækken
 
flickr's architecture & php
flickr's architecture & php flickr's architecture & php
flickr's architecture & php coolpics
 
The Solar Framework for PHP
The Solar Framework for PHPThe Solar Framework for PHP
The Solar Framework for PHPConFoo
 
Drupal and the rise of the documents
Drupal and the rise of the documentsDrupal and the rise of the documents
Drupal and the rise of the documentsClaudio Beatrice
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyNick Sieger
 
Rake: Not Your Father's Build Tool
Rake: Not Your Father's Build ToolRake: Not Your Father's Build Tool
Rake: Not Your Father's Build Toolfilmprog
 
An introduction to Rails 3
An introduction to Rails 3An introduction to Rails 3
An introduction to Rails 3Blazing Cloud
 
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Nilesh Panchal
 
Apcug 2011 07-17-intro_to_drupal_jeff_schuler
Apcug 2011 07-17-intro_to_drupal_jeff_schulerApcug 2011 07-17-intro_to_drupal_jeff_schuler
Apcug 2011 07-17-intro_to_drupal_jeff_schulerhewie
 
Introducing ruby on rails
Introducing ruby on railsIntroducing ruby on rails
Introducing ruby on railsPriceen
 
Architecture by Accident
Architecture by AccidentArchitecture by Accident
Architecture by AccidentGleicon Moraes
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteDr Nic Williams
 

Similar to Rails ORM De-mystifying Active Record has_many (20)

Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex
 
flickr's architecture & php
flickr's architecture & php flickr's architecture & php
flickr's architecture & php
 
The Solar Framework for PHP
The Solar Framework for PHPThe Solar Framework for PHP
The Solar Framework for PHP
 
Drupal and the rise of the documents
Drupal and the rise of the documentsDrupal and the rise of the documents
Drupal and the rise of the documents
 
Rails traps
Rails trapsRails traps
Rails traps
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
 
Rake: Not Your Father's Build Tool
Rake: Not Your Father's Build ToolRake: Not Your Father's Build Tool
Rake: Not Your Father's Build Tool
 
A Practical Multi-Tenant Cluster
A Practical Multi-Tenant ClusterA Practical Multi-Tenant Cluster
A Practical Multi-Tenant Cluster
 
Pocket Knife JS
Pocket Knife JSPocket Knife JS
Pocket Knife JS
 
An introduction to Rails 3
An introduction to Rails 3An introduction to Rails 3
An introduction to Rails 3
 
Bhavesh ro r
Bhavesh ro rBhavesh ro r
Bhavesh ro r
 
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
 
HTML5 and Sencha Touch
HTML5 and Sencha TouchHTML5 and Sencha Touch
HTML5 and Sencha Touch
 
Apcug 2011 07-17-intro_to_drupal_jeff_schuler
Apcug 2011 07-17-intro_to_drupal_jeff_schulerApcug 2011 07-17-intro_to_drupal_jeff_schuler
Apcug 2011 07-17-intro_to_drupal_jeff_schuler
 
Ruby on rails for beginers
Ruby on rails for beginersRuby on rails for beginers
Ruby on rails for beginers
 
JRubyConf 2009
JRubyConf 2009JRubyConf 2009
JRubyConf 2009
 
Introducing ruby on rails
Introducing ruby on railsIntroducing ruby on rails
Introducing ruby on rails
 
Architecture by Accident
Architecture by AccidentArchitecture by Accident
Architecture by Accident
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - Keynote
 
API Design
API DesignAPI Design
API Design
 

More from Blazing Cloud

More from Blazing Cloud (20)

RSpec Quick Reference
RSpec Quick ReferenceRSpec Quick Reference
RSpec Quick Reference
 
Extending rails
Extending railsExtending rails
Extending rails
 
2day Ruby Class Intro
2day Ruby Class Intro2day Ruby Class Intro
2day Ruby Class Intro
 
Mobile Lean UX
Mobile Lean UXMobile Lean UX
Mobile Lean UX
 
Interactive Graphics
Interactive GraphicsInteractive Graphics
Interactive Graphics
 
Interactive Graphics w/ Javascript, HTML5 and CSS3
Interactive Graphics w/ Javascript, HTML5 and CSS3Interactive Graphics w/ Javascript, HTML5 and CSS3
Interactive Graphics w/ Javascript, HTML5 and CSS3
 
Form helpers
Form helpersForm helpers
Form helpers
 
What you don't know (yet)
What you don't know (yet)What you don't know (yet)
What you don't know (yet)
 
Introduction to Rails
Introduction to RailsIntroduction to Rails
Introduction to Rails
 
ActiveRecord
ActiveRecordActiveRecord
ActiveRecord
 
Ruby on Rails Class intro
Ruby on Rails Class introRuby on Rails Class intro
Ruby on Rails Class intro
 
Ruby on rails toolbox
Ruby on rails toolboxRuby on rails toolbox
Ruby on rails toolbox
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Active Record
Active RecordActive Record
Active Record
 
Reg EX
Reg EXReg EX
Reg EX
 
Files IO
Files IOFiles IO
Files IO
 
Ruby Blocks
Ruby BlocksRuby Blocks
Ruby Blocks
 
Power Ruby
Power RubyPower Ruby
Power Ruby
 
Rails 3 ActiveRecord
Rails 3 ActiveRecordRails 3 ActiveRecord
Rails 3 ActiveRecord
 
Iterators, Hashes, and Arrays
Iterators, Hashes, and ArraysIterators, Hashes, and Arrays
Iterators, Hashes, and Arrays
 

Recently uploaded

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaRTTS
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Product School
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesThousandEyes
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Product School
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaCzechDreamin
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...Product School
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka DoktorováCzechDreamin
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekCzechDreamin
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonDianaGray10
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesBhaskar Mitra
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...CzechDreamin
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...CzechDreamin
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupCatarinaPereira64715
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualityInflectra
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlPeter Udo Diehl
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCzechDreamin
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...Product School
 

Recently uploaded (20)

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 

Rails ORM De-mystifying Active Record has_many

  • 1. RAILS ORM de-mystifying ActiveRecord Thursday, December 8, 11
  • 2. NoteToSelf: Know your audience • Who has used an ORM? • Daily: • Who uses Relational Databases? • Who uses Java,Erlang,ObjectiveC? • Who uses PHP, Python? • Who uses Javascript? • Who uses [Perl, Smalltalk, Lisp, Ruby,] Thursday, December 8, 11
  • 3. NTS: Address a Need What I want => • How can I do my job smoother, faster, and to a higher standard? What I need first => • Where can I can get a ‘run-time’ model of rails? Thursday, December 8, 11
  • 4. NTS: Fulfill need • What does Rails do for you? • What do you need to do for Rails? Rails is Ruby, Ruby is Awesome, Transitive Relation , Rails is Awesome. Thursday, December 8, 11
  • 5. What you are about to see • Papa Bears Honey Sharing Co-Operative Thursday, December 8, 11
  • 6. Papa Bear Honey Share Thursday, December 8, 11
  • 7. What you are about to see • (2) one-to-many relationships Thursday, December 8, 11
  • 9. What you are about to see • External Resource Scheme Thursday, December 8, 11
  • 10. External REST Resource API Thursday, December 8, 11
  • 12. What you are about to see • database model implies methods Thursday, December 8, 11
  • 16. module ActiveRecord   module ConnectionAdapters class SQLiteAdapter < AbstractAdapter def table_structure(table_name)   structure = exec_query("PRAGMA table_info(#{quote_table_name(table_name)})", 'SCHEMA').to_hash   raise(ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'") if structure.empty? structure end https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb Thursday, December 8, 11
  • 17. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_methods.rb for all table attributes define a method to change attribute to read attribute story! Thursday, December 8, 11
  • 18. What you are about to see • Inserting a bunch of pre declared data via External API • Hand waving Thursday, December 8, 11
  • 19. rails server & Thursday, December 8, 11
  • 20. cat curl.txt sh curl.txt Thursday, December 8, 11
  • 22. What you are about to see • ‘has_many’ declaring an association to ‘honeys’ Thursday, December 8, 11
  • 23. app/models/hive.rb class Hive < ActiveRecord::Base belongs_to :field_of_origin has_many :honeys # product end Thursday, December 8, 11
  • 24. activerecord/lib/active_record/associations.rb module ActiveRecord module Associations  def has_many(name, options = {}, &extension)   Builder::HasMany.build(self, name, options, &extension)  end https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations.rb Thursday, December 8, 11
  • 25. activerecord/lib/active_record/associations/builder/association.rb module ActiveRecord::Associations::Builder   class Association def self.build(model, name, options) new(model, name, options).build end def define_readers   name = self.name   mixin.redefine_method(name) do |*params|   association(name).reader(*params) end end https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/association.rb Thursday, December 8, 11
  • 26. What you are about to see • ‘honeys’ reader method • ‘has_many’ builds methods like ‘create!’ on the reader association Thursday, December 8, 11
  • 27. app/controllers/honeys_controller.rb class HoneysController < ApplicationController def create current_hive = Hive.find(params[:hive_id]) if current_hive current_hive.honeys.create!(params[:honey]) render :json => current_hive.to_json(:include => :honeys) else head :error end end end Thursday, December 8, 11
  • 28. activerecord/lib/active_record/associations/collection_association.rb module ActiveRecord   module Associations     class CollectionAssociation < Association def create!(attributes = {}, options = {}, &block) create_record(attributes, options, true, &block) end https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/collection_association.rb Thursday, December 8, 11
  • 29. SEE CURL POST curl -d 'honey[name]=Cinnamon%20Manuka%20&honey[volume_in_ml]=2520' http://localhost:3000/hives/1/honeys Thursday, December 8, 11
  • 30. What does Rails ORM do for me? • Keeps me focused on describing the domain • Keeps me from re-expresing my data model in query/strings/logic/controllers/libraries • Keeps me from having to write generic code - focus on what is specific. Thursday, December 8, 11
  • 31. What do I need to do for Rails ORM? • learn Rails conventions • learn Rails limits • express my design in it’s language Thursday, December 8, 11
  • 32. Where can I get a Realtime / Runtime understanding of what Rails is doing? • practice • errors • reflection • rinse & repeat Thursday, December 8, 11