Your SlideShare is downloading. ×
0
RAILS ORM             de-mystifying ActiveRecordThursday, December 8, 11
NoteToSelf: Know your audience                    •      Who has used an ORM?                    •      Daily:            ...
NTS: Address a Need What I want =>                    • How can I do my job smoother, faster, and                         ...
NTS: Fulfill need                    • What does Rails do for you?                    • What do you need to do for Rails?  ...
What you are about to see                    • Papa Bears Honey Sharing Co-OperativeThursday, December 8, 11
Papa Bear Honey ShareThursday, December 8, 11
What you are about to see                    • (2) one-to-many relationshipsThursday, December 8, 11
Internal Data ModelThursday, December 8, 11
What you are about to see                    • External Resource SchemeThursday, December 8, 11
External REST Resource APIThursday, December 8, 11
git@github.com:blazingcloud/papabear.gitThursday, December 8, 11
What you are about to see                    • database model implies methodsThursday, December 8, 11
rake db:resetThursday, December 8, 11
rails dbThursday, December 8, 11
PRAGMA table_info(hives);Thursday, December 8, 11
module ActiveRecord                             module ConnectionAdapters                               class SQLiteAdapte...
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_methods.rb                            ...
What you are about to see                    • Inserting a bunch of pre declared data via                           Extern...
rails server &Thursday, December 8, 11
cat curl.txt                           sh curl.txtThursday, December 8, 11
http://localhost:3000/Thursday, December 8, 11
What you are about to see                    • ‘has_many’ declaring an association to                           ‘honeys’Th...
app/models/hive.rb          class Hive < ActiveRecord::Base            belongs_to :field_of_origin            has_many :ho...
activerecord/lib/active_record/associations.rb                                module ActiveRecord                         ...
activerecord/lib/active_record/associations/builder/association.rb                           module ActiveRecord::Associat...
What you are about to see                    • ‘honeys’ reader method                    • ‘has_many’ builds methods like ...
app/controllers/honeys_controller.rb          class HoneysController < ApplicationController            def create        ...
activerecord/lib/active_record/associations/collection_association.rb                           module ActiveRecord       ...
SEE CURL POST        curl -d honey[name]=Cinnamon%20Manuka%20&honey[volume_in_ml]=2520 http://localhost:3000/hives/1/honey...
What does Rails ORM do for me?                    • Keeps me focused on describing the                           domain   ...
What do I need to do for Rails ORM?                    • learn Rails conventions                    • learn Rails limits  ...
Where can I get a Realtime / Runtime                           understanding of what Rails is doing?                    • ...
quest-ions?Thursday, December 8, 11
Upcoming SlideShare
Loading in...5
×

Rails ORM De-mystifying Active Record has_many

1,287

Published on

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!
=> NoMethodError

Hive.first.honeys.create!

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,287
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Rails ORM De-mystifying Active Record has_many"

  1. 1. RAILS ORM de-mystifying ActiveRecordThursday, December 8, 11
  2. 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. 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. 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. 5. What you are about to see • Papa Bears Honey Sharing Co-OperativeThursday, December 8, 11
  6. 6. Papa Bear Honey ShareThursday, December 8, 11
  7. 7. What you are about to see • (2) one-to-many relationshipsThursday, December 8, 11
  8. 8. Internal Data ModelThursday, December 8, 11
  9. 9. What you are about to see • External Resource SchemeThursday, December 8, 11
  10. 10. External REST Resource APIThursday, December 8, 11
  11. 11. git@github.com:blazingcloud/papabear.gitThursday, December 8, 11
  12. 12. What you are about to see • database model implies methodsThursday, December 8, 11
  13. 13. rake db:resetThursday, December 8, 11
  14. 14. rails dbThursday, December 8, 11
  15. 15. PRAGMA table_info(hives);Thursday, December 8, 11
  16. 16. module ActiveRecord   module ConnectionAdapters class SQLiteAdapter < AbstractAdapterdef table_structure(table_name)  structure = exec_query("PRAGMAtable_info(#{quote_table_name(table_name)})",SCHEMA).to_hash  raise(ActiveRecord::StatementInvalid, "Could notfind table #{table_name}") if structure.empty? structureend https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rbThursday, December 8, 11
  17. 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. 18. What you are about to see • Inserting a bunch of pre declared data via External API • Hand wavingThursday, December 8, 11
  19. 19. rails server &Thursday, December 8, 11
  20. 20. cat curl.txt sh curl.txtThursday, December 8, 11
  21. 21. http://localhost:3000/Thursday, December 8, 11
  22. 22. What you are about to see • ‘has_many’ declaring an association to ‘honeys’Thursday, December 8, 11
  23. 23. app/models/hive.rb class Hive < ActiveRecord::Base belongs_to :field_of_origin has_many :honeys # product endThursday, December 8, 11
  24. 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.rbThursday, December 8, 11
  25. 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.rbThursday, December 8, 11
  26. 26. What you are about to see • ‘honeys’ reader method • ‘has_many’ builds methods like ‘create!’ on the reader associationThursday, December 8, 11
  27. 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 endThursday, December 8, 11
  28. 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.rbThursday, December 8, 11
  29. 29. SEE CURL POST curl -d honey[name]=Cinnamon%20Manuka%20&honey[volume_in_ml]=2520 http://localhost:3000/hives/1/honeysThursday, December 8, 11
  30. 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. 31. What do I need to do for Rails ORM? • learn Rails conventions • learn Rails limits • express my design in it’s languageThursday, December 8, 11
  32. 32. Where can I get a Realtime / Runtime understanding of what Rails is doing? • practice • errors • reflection • rinse & repeatThursday, December 8, 11
  33. 33. quest-ions?Thursday, December 8, 11
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×