SlideShare una empresa de Scribd logo
1 de 33
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

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
Henry S
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
Manoj Kumar
 
Flickr Architecture Presentation
Flickr Architecture PresentationFlickr Architecture Presentation
Flickr Architecture Presentation
eraz
 

La actualidad más candente (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
 

Destacado (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 a Rails ORM De-mystifying Active Record has_many

The Solar Framework for PHP
The Solar Framework for PHPThe Solar Framework for PHP
The Solar Framework for PHP
ConFoo
 
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
 
Introducing ruby on rails
Introducing ruby on railsIntroducing ruby on rails
Introducing ruby on rails
Priceen
 

Similar a 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
 

Más de Blazing Cloud

RSpec Quick Reference
RSpec Quick ReferenceRSpec Quick Reference
RSpec Quick Reference
Blazing Cloud
 
Interactive Graphics
Interactive GraphicsInteractive Graphics
Interactive Graphics
Blazing Cloud
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Blazing Cloud
 

Más de 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
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

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