rails orm de-mystifying active record has_many
DESCRIPTION
Rails' ORM layer, ActiveRecord, is an elegant solution for keeping model code simple and modular (aka DRY). Demystifying the way Ruby-on-Rails uses runtime method generation opens a doorway for understanding and provides a foundation for the other ways Rails uses simple conventions to allow sophisticated, concise functionality in a declarative style.Here is big mystery that you'll be equipped to understand better after playing with the slides-> If honeys is an array - and honeys has a method create! - then why does an array object [] not have create!Hive.first.honeys.class => Array[].create! => NoMethodErrorHive.first.honeys.create!TRANSCRIPT
![Page 1: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/1.jpg)
RAILS ORM de-mystifying ActiveRecord
Thursday, December 8, 11
![Page 2: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/2.jpg)
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
![Page 3: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/3.jpg)
NTS: Address a Need
• How can I do my job smoother, faster, and to a higher standard?
• Where can I can get a ‘run-time’ model of rails?
What I want =>
What I need first =>
Thursday, December 8, 11
![Page 4: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/4.jpg)
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
![Page 5: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/5.jpg)
What you are about to see
• Papa Bears Honey Sharing Co-Operative
Thursday, December 8, 11
![Page 6: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/6.jpg)
Papa Bear Honey Share
Thursday, December 8, 11
![Page 7: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/7.jpg)
What you are about to see
• (2) one-to-many relationships
Thursday, December 8, 11
![Page 8: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/8.jpg)
Internal Data Model
Thursday, December 8, 11
![Page 9: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/9.jpg)
What you are about to see
• External Resource Scheme
Thursday, December 8, 11
![Page 10: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/10.jpg)
External REST Resource API
Thursday, December 8, 11
![Page 12: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/12.jpg)
What you are about to see
• database model implies methods
Thursday, December 8, 11
![Page 13: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/13.jpg)
rake db:reset
Thursday, December 8, 11
![Page 14: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/14.jpg)
rails db
Thursday, December 8, 11
![Page 15: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/15.jpg)
PRAGMA table_info(hives);
Thursday, December 8, 11
![Page 16: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/16.jpg)
module ActiveRecord module ConnectionAdapters class SQLiteAdapter < AbstractAdapter
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
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? structureend
Thursday, December 8, 11
![Page 17: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/17.jpg)
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
![Page 18: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/18.jpg)
What you are about to see
• Inserting a bunch of pre declared data via External API
• Hand waving
Thursday, December 8, 11
![Page 19: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/19.jpg)
rails server &
Thursday, December 8, 11
![Page 20: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/20.jpg)
cat curl.txtsh curl.txt
Thursday, December 8, 11
![Page 22: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/22.jpg)
What you are about to see
• ‘has_many’ declaring an association to ‘honeys’
Thursday, December 8, 11
![Page 23: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/23.jpg)
app/models/hive.rb
class Hive < ActiveRecord::Base belongs_to :field_of_origin has_many :honeys # productend
Thursday, December 8, 11
![Page 24: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/24.jpg)
def has_many(name, options = {}, &extension) Builder::HasMany.build(self, name, options, &extension) end
module ActiveRecord module Associations
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations.rb
activerecord/lib/active_record/associations.rb
Thursday, December 8, 11
![Page 25: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/25.jpg)
module ActiveRecord::Associations::Builder class Association
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/association.rb
activerecord/lib/active_record/associations/builder/association.rb
def self.build(model, name, options) new(model, name, options).buildend
def define_readers name = self.name mixin.redefine_method(name) do |*params| association(name).reader(*params) endend
Thursday, December 8, 11
![Page 26: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/26.jpg)
What you are about to see
• ‘honeys’ reader method
• ‘has_many’ builds methods like ‘create!’ on the reader association
Thursday, December 8, 11
![Page 27: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/27.jpg)
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 endend
Thursday, December 8, 11
![Page 28: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/28.jpg)
module ActiveRecord module Associations class CollectionAssociation < Association
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/collection_association.rb
def create!(attributes = {}, options = {}, &block) create_record(attributes, options, true, &block)end
activerecord/lib/active_record/associations/collection_association.rb
Thursday, December 8, 11
![Page 29: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/29.jpg)
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
![Page 30: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/30.jpg)
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
![Page 31: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/31.jpg)
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
![Page 32: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/32.jpg)
Where can I get a Realtime / Runtime understanding of what Rails is doing?
• practice
• errors
• reflection
• rinse & repeat
Thursday, December 8, 11
![Page 33: Rails ORM De-mystifying Active Record has_many](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5550d586b4c905f2318b5071/html5/thumbnails/33.jpg)
quest-ions?
Thursday, December 8, 11