ActiveRecord Associations (2), Season 1

3,195 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,195
On SlideShare
0
From Embeds
0
Number of Embeds
1,971
Actions
Shares
0
Downloads
31
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • ActiveRecord Associations (2), Season 1

    1. 1. The 8th Round of ROR Lab.Active RecordAssociations - 2 March 3rd, 2012 Hyoseong Choi ROR Lab.
    2. 2. Short Review ROR Lab.
    3. 3. For what? Primary & Foreign Keyscustomer order id id ROR Lab.
    4. 4. For what? Primary & Foreign Keyscustomer order id id customer_idparent obj. child obj. ROR Lab.
    5. 5. For what? Primary & Foreign Keyscustomer order id id Active Record customer_id Assocationparent obj. child obj. ROR Lab.
    6. 6. For what? ROR Lab.
    7. 7. For what?To add an order ROR Lab.
    8. 8. For what?To add an order ROR Lab.
    9. 9. For what?To add an orderTo delete a customer ROR Lab.
    10. 10. For what?To add an orderTo delete a customer ROR Lab.
    11. 11. Associations Parent Class Child Class has_one1 :1 belongs_to has_one, :through1 :n has_many belongs_to has_many A has_many B, :through belongs_ton :m has_many B has_many A, :through has_and_belongs_to_many ROR Lab.
    12. 12. Association Optionsbelongs_to options has_one options has_many options HABTM options• :autosave • :as • :as • :association_foreign_key• :class_name • :autosave • :autosave • :autosave• :conditions • :class_name • :class_name • :class_name• :counter_cache • :conditions • :conditions • :conditions• :dependent • :dependent • :counter_sql • :counter_sql• :foreign_key • :foreign_key • :dependent • :delete_sql• :include • :include • :extend • :extend• :polymorphic • :order • :finder_sql • :finder_sql• :readonly • :primary_key • :foreign_key • :foreign_key• :select • :readonly • :group • :group• :touch • :select • :include • :include• :validate • :source • :limit • :insert_sql • :source_type • :offset • :join_table • :through • :order • :limit • :validate • :primary_key • :offset • :readonly • :order • :select • :readonly • :source • :select • :source_type • :uniq • :through • :validate • :uniq • :validate ROR Lab.
    13. 13. Polymorphic Imageable Picture id imageable_id Imageable Picture id imageable_id ROR Lab.
    14. 14. Polymorphic Imageable Picture Employee id id imageable_id Imageable Picture Product id id imageable_id ROR Lab.
    15. 15. :counter_cache• But, you should add “orders_count” column to Customer model• You can override the default column name.• ReadOnly !!! ROR Lab.
    16. 16. :counter_cache• But, you should add “orders_count” column to Customer model• You can override the default column name.• ReadOnly !!! ROR Lab.
    17. 17. :touch ROR Lab.
    18. 18. :touch ROR Lab.
    19. 19. :touchOr using a custom attribute ROR Lab.
    20. 20. :touchOr using a custom attribute ROR Lab.
    21. 21. ActiveRecordAssociation - 2 ROR Lab.
    22. 22. Association Optionsbelongs_to options has_one options has_many options HABTM options• :autosave • :autosave • :autosave • :autosave• :class_name • :class_name • :class_name • :class_name• :conditions • :conditions • :conditions • :conditions• :foreign_key • :foreign_key • :foreign_key • :foreign_key• :include • :include • :include • :include• :readonly • :readonly • :readonly • :readonly• :select • :select • :select • :select• :validate • :validate • :validate • :validate• :dependent • :as • :as • :order• :counter_cache • :order • :order • :counter_sql• :polymorphic • :primary_key • :primary_key • :extend• :touch • :source • :source • :finder_sql • :source_type • :source_type • :group • :through • :through • :limit • :dependent • :counter_sql • :offset • :extend • :uniq • :finder_sql • :association_foreign_key • :group • :delete_sql • :limit • :insert_sql • :offset • :join_table • :uniq • :dependent ROR Lab.
    23. 23. Association Optionsbelongs_to options has_one options has_many options HABTM options• :autosave • :autosave • :autosave • :autosave• :class_name • :class_name • :class_name • :class_name• :conditions • :conditions • :conditions • :conditions• :foreign_key • :foreign_key • :foreign_key • :foreign_key• :include • :include • :include • :include• :readonly • :readonly • :readonly • :readonly• :select • :select • :select • :select• :validate • :validate • :validate • :validate• :dependent • :as • :as • :order• :counter_cache • :order • :order • :counter_sql• :polymorphic • :primary_key • :primary_key • :extend• :touch • :source • :source • :finder_sql • :source_type • :source_type • :group • :through • :through • :limit • :dependent • :counter_sql • :offset • :extend • :uniq • :finder_sql • :association_foreign_key • :group • :delete_sql • :limit • :insert_sql • :offset • :join_table • :uniq • :dependent ROR Lab.
    24. 24. Association Optionsbelongs_to options has_one options has_many options HABTM options• :autosave • :autosave • :autosave • :autosave• :class_name • :class_name • :class_name • :class_name• :conditions • :conditions • :conditions • :conditions• :foreign_key • :foreign_key • :foreign_key • :foreign_key• :include • :include • :include • :include• :readonly • :readonly • :readonly • :readonly• :select • :select • :select • :select• :validate • :validate • :validate • :validate• :dependent • :as • :as • :order• :counter_cache • :order • :order • :counter_sql• :polymorphic • :primary_key • :primary_key • :extend• :touch • :source • :source • :finder_sql • :source_type • :source_type • :group • :through • :through • :limit • :dependent • :counter_sql • :offset • :extend • :uniq • :finder_sql • :association_foreign_key • :group • :delete_sql • :limit • :insert_sql • :offset • :join_table • :uniq • :dependent ROR Lab.
    25. 25. Association Optionsbelongs_to options has_one options has_many options HABTM options• :autosave • :autosave • :autosave • :autosave• :class_name • :class_name • :class_name • :class_name• :conditions • :conditions • :conditions • :conditions• :foreign_key • :foreign_key • :foreign_key • :foreign_key• :include • :include • :include • :include• :readonly • :readonly • :readonly • :readonly• :select • :select • :select • :select• :validate • :validate • :validate • :validate• :dependent • :as • :as • :order• :counter_cache • :order • :order • :counter_sql• :polymorphic • :primary_key • :primary_key • :extend• :touch • :source • :source • :finder_sql • :source_type • :source_type • :group • :through • :through • :limit • :dependent • :counter_sql • :offset • :extend • :uniq • :finder_sql • :association_foreign_key • :group • :delete_sql • :limit • :insert_sql • :offset • :join_table • :uniq • :dependent ROR Lab.
    26. 26. Association Optionsbelongs_to options has_one options has_many options HABTM options• :autosave • :autosave • :autosave • :autosave• :class_name • :class_name • :class_name • :class_name• :conditions • :conditions • :conditions • :conditions• :foreign_key • :foreign_key • :foreign_key • :foreign_key• :include • :include • :include • :include• :readonly • :readonly • :readonly • :readonly• :select • :select • :select • :select• :validate • :validate • :validate • :validate• :dependent • :as • :as • :order• :counter_cache • :order • :order • :counter_sql• :polymorphic • :primary_key • :primary_key • :extend• :touch • :source • :source • :finder_sql • :source_type • :source_type • :group • :through • :through • :limit • :dependent • :counter_sql • :offset • :extend • :uniq • :finder_sql • :association_foreign_key • :group • :delete_sql • :limit • :insert_sql • :offset • :join_table • :uniq • :dependent ROR Lab.
    27. 27. Association Optionsbelongs_to options has_one options has_many options HABTM options• :autosave • :autosave • :autosave • :autosave• :class_name • :class_name • :class_name • :class_name• :conditions • :conditions • :conditions • :conditions• :foreign_key • :foreign_key • :foreign_key • :foreign_key• :include • :include • :include • :include• :readonly • :readonly • :readonly • :readonly• :select • :select • :select • :select• :validate • :validate • :validate • :validate• :dependent • :as • :as • :order• :counter_cache • :order • :order • :counter_sql• :polymorphic • :primary_key • :primary_key • :extend• :touch • :source • :source • :finder_sql • :source_type • :source_type • :group • :through • :through • :limit • :dependent • :counter_sql • :offset • :extend • :uniq • :finder_sql • :association_foreign_key • :group • :delete_sql • :limit • :insert_sql • :offset • :join_table • :uniq • :dependent ROR Lab.
    28. 28. Association Optionsbelongs_to options has_one options has_many options HABTM options• :autosave • :autosave • :autosave • :autosave• :class_name • :class_name • :class_name • :class_name• :conditions • :conditions • :conditions • :conditions• :foreign_key • :foreign_key • :foreign_key • :foreign_key• :include • :include • :include • :include• :readonly • :readonly • :readonly • :readonly• :select • :select • :select • :select• :validate • :validate • :validate • :validate• :dependent • :as • :as • :order• :counter_cache • :order • :order • :counter_sql• :polymorphic • :primary_key • :primary_key • :extend• :touch • :source • :source • :finder_sql • :source_type • :source_type • :group • :through • :through • :limit • :dependent • :counter_sql • :offset • :extend • :uniq • :finder_sql • :association_foreign_key • :group • :delete_sql • :limit • :insert_sql • :offset • :join_table • :uniq • :dependent ROR Lab.
    29. 29. Association Optionsbelongs_to options has_one options has_many options HABTM options• :autosave • :autosave • :autosave • :autosave• :class_name • :class_name • :class_name • :class_name• :conditions • :conditions • :conditions • :conditions• :foreign_key • :foreign_key • :foreign_key • :foreign_key• :include • :include • :include • :include• :readonly • :readonly • :readonly • :readonly• :select • :select • :select • :select• :validate • :validate • :validate • :validate• :dependent • :as • :as • :order• :counter_cache • :order • :order • :counter_sql• :polymorphic • :primary_key • :primary_key • :extend• :touch • :source • :source • :finder_sql • :source_type • :source_type • :group • :through • :through • :limit • :dependent • :counter_sql • :offset • :extend • :uniq • :finder_sql • :association_foreign_key • :group • :delete_sql • :limit • :insert_sql • :offset • :join_table • :uniq • :dependent ROR Lab.
    30. 30. Common Options• :autosave• :class_name• :conditions• :foreign_key• :include• :readonly• :select• :validate ROR Lab.
    31. 31. - common options - :autosave • No declaration (default) : new children are saved when their parent is saved • :autosave => true : all children is saved, no matter whether they are new records • :autosave => false : any children is not saved ROR Lab.
    32. 32. - common options - :autosave class Post has_one :author, :autosave => true end has_one post = Post.find(1) post.title # => "The current global position of migrating ducks" post.author.name # => "alloy" post.title = "On the migration of ducks" post.author.name = "Eloy Duran" post.save post.reload post.title # => "On the migration of ducks" post.author.name # => "Eloy Duran" post.author.mark_for_destruction post.author.marked_for_destruction? # => true id = post.author.id Author.find_by_id(id).nil? # => false post.save post.reload.author # => nil Author.find_by_id(id).nil? # => true http://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html ROR Lab.
    33. 33. - common options - :autosave has_many class Post has_many :comments # :autosave option is no declared end post = Post.new(:title => ruby rocks) post.comments.build(:body => hello world) post.save # => saves both post and comment post = Post.create(:title => ruby rocks) post.comments.build(:body => hello world) post.save # => saves both post and comment post = Post.create(:title => ruby rocks) post.comments.create(:body => hello world) post.save # => saves both post and comment class Post has_many :comments, :autosave => true end post = Post.create(:title => ruby rocks) post.comments.create(:body => hello world) post.comments[0].body = hi everyone post.save # => saves both post and comment, with hi everyone as body post.comments.last.mark_for_destruction post.comments.last.marked_for_destruction? # => true post.comments.length # => 2 id = post.comments.last.id Comment.find_by_id(id).nil? # => false post.save post.reload.comments.length # => 1 Comment.find_by_id(id).nil? # => true ROR Lab.
    34. 34. - common options - :class_name class Order < ActiveRecord::Base   belongs_to :customer, :class_name => "Patron" end ROR Lab.
    35. 35. - common options - :conditions class Order < ActiveRecord::Base   belongs_to :customer, :conditions => "active = 1" end ROR Lab.
    36. 36. - common options - :foreign_key class Order < ActiveRecord::Base   belongs_to :customer, :class_name => "Patron",     :foreign_key => "patron_id" end ROR Lab.
    37. 37. - common options - :include class LineItem < ActiveRecord::Base   belongs_to :order, :include => :customer end   class Order < ActiveRecord::Base   belongs_to :customer   has_many :line_items end   class Customer < ActiveRecord::Base   has_many :orders end ROR Lab.
    38. 38. - common options - :readonly class Customer < ActiveRecord::Base   has_many :orders, :readonly => true end ROR Lab.
    39. 39. - common options - :select class Order < ActiveRecord::Base   belongs_to :customer, :select => "name, profile, group_id" end ROR Lab.
    40. 40. - common options - :validate class Order < ActiveRecord::Base   belongs_to :customer, :validate => true end ROR Lab.
    41. 41. Common has_ Options has_one / has_many• :as• :order* (also, included in HABTM)• :primary_key• :source• :source_type• :through ROR Lab.
    42. 42. - common has_ options - :as class Picture < ActiveRecord::Base   belongs_to :imageable, :polymorphic => true end   class Employee < ActiveRecord::Base   has_many :pictures, :as => :imageable end   class Product < ActiveRecord::Base   has_many :pictures, :as => :imageable end ROR Lab.
    43. 43. - common has_ options - :order class Post < ActiveRecord::Base   has_many :comments, :order => ”updated_at” end •also, in HABTM ROR Lab.
    44. 44. - common has_ options - :primary_key class Order < ActiveRecord::Base default   belongs_to :customer, :primary_key => "id", :foreign_key => "customer_id" class Order < ActiveRecord::Base   belongs_to :customer, :class_name => "Patron", :primary_key => "civil_no", :foreign_key => "patron_id" ROR Lab.
    45. 45. - common has_ options - :source & :source_type class Tag < ActiveRecord::Base has_many :taggings, :dependent => :destroy has_many :books, :through => :tagings, :source => :taggable, :source_type => "Book" has_many :movies, :through => :tagings, :source => :taggable, :source_type => "Movie" end class Tagging < ActiveRecord::Base belongs_to :taggable, :polymorphic => true belongs_to :tag end class Book < ActiveRecord::Base has_many :taggings, :as => :taggable has_many :tags, :through => :taggings end class Movie < ActiveRecord::Base has_many :taggings, :as => :taggable has_many :tags, :through => :taggings end • http://www.brentmc79.com/posts/polymorphic-many-to-many-associations-in-rails ROR Lab.
    46. 46. - common has_ options - :through class Supplier < ActiveRecord::Base   has_one :account   has_one :account_history, :through => :account end   class Account < ActiveRecord::Base   belongs_to :supplier   has_one :account_history end   class AccountHistory < ActiveRecord::Base   belongs_to :account end ROR Lab.
    47. 47. Common _many Options has_many / HABTM• :counter_sql• :extend• :finder_sql• :group• :limit• :offset• :uniq ROR Lab.
    48. 48. - common _many options - :finder_sql class Person < ActiveRecord::Base has_many :subscribers, :class_name => "Person", :finder_sql => Proc.new { %Q{ SELECT DISTINCT * FROM people p, post_subscriptions ps WHERE ps.post_id = #{id} AND ps.person_id = p.id ORDER BY p.first_name } } ROR Lab.
    49. 49. - common _many options - :counter_sql class Person < ActiveRecord::Base has_many :subscribers, :class_name => "Person", :counter_sql => Proc.new { %Q{ SELECT DISTINCT * FROM people p, post_subscriptions ps WHERE ps.post_id = #{id} AND ps.person_id = p.id ORDER BY p.first_name } } ROR Lab.
    50. 50. - common _many options - :extend association proxy(or interface) class Customer < ActiveRecord::Base   has_many :orders do     def find_by_order_prefix(order_number)       find_by_region_id(order_number[0..2])     end   end ROR Lab.
    51. 51. - common _many options - :extend module FindRecentExtension   def find_recent     where("created_at > ?", 5.days.ago)   end end   class Customer < ActiveRecord::Base   has_many :orders, :extend => FindRecentExtension end   class Supplier < ActiveRecord::Base   has_many :deliveries, :extend => FindRecentExtension ROR Lab.
    52. 52. - common _many options - :extend class Customer < ActiveRecord::Base   has_many :orders,     :extend => [FindRecentExtension, FindActiveExtension] end ROR Lab.
    53. 53. - common _many options - * 3 AP accessors • proxy_association.owner • proxy_association.reflection • proxy_association.target AP* : Association Proxy ROR Lab.
    54. 54. - common _many options - :group class Customer < ActiveRecord::Base   has_many :line_items, :through => :orders, :group => "orders.id" end ROR Lab.
    55. 55. - common _many options - :limit / :offset class Customer < ActiveRecord::Base   has_many :recent_orders, :class_name => "Order",      :order => "order_date DESC", :limit => 100 :offset => 300 ROR Lab.
    56. 56. - common _many options - :uniq class Person < ActiveRecord::Base   has_many :readings   has_many :posts, :through => :readings end   person = Person.create(:name => john) post   = Post.create(:name => a1) person.posts << post person.posts << post person.posts.inspect # => [#<Post id: 5, name: "a1">, #<Post id: 5, name: "a1">] Reading.all.inspect  # => [#<Reading id: 12, person_id: 5, post_id: 5>, #<Reading id: 13, person_id: 5, post_id: 5>] ROR Lab.
    57. 57. - common _many options - :uniq class Person   has_many :readings   has_many :posts, :through => :readings, :uniq => true end   person = Person.create(:name => honda) post   = Post.create(:name => a1) person.posts << post person.posts << post person.posts.inspect # => [#<Post id: 7, name: "a1">] Reading.all.inspect  # => [#<Reading id: 16, person_id: 7, post_id: 7>, #<Reading id: 17, person_id: 7, post_id: 7>] ROR Lab.
    58. 58. Common one-way Options belongs_to / has_one / has_many• :dependent class Comment < ActiveRecord::Base   belongs_to :post, :dependent => :destroy end class Post < ActiveRecord::Base   has_many :comments, :dependent => :destroy end ROR Lab.
    59. 59. Special Options• belongs_to • :counter_cache • :polymorphic • :touch• HABTM • :association_foreign_key • :delete_sql • :insert_sql • :join_table ROR Lab.
    60. 60. - special options for HABTM - :association_foreign_key class User < ActiveRecord::Base   has_and_belongs_to_many :friends, :class_name => "User",     :foreign_key => "this_user_id",     :association_foreign_key => "other_user_id" many-to-many self join ROR Lab.
    61. 61. - special options for HABTM - :delete_sql class Developer < ActiveRecord::Base has_and_belongs_to_many :active_projects, :join_table => developers_projects, :delete_sql => "DELETE FROM developers_projects WHERE active=1 AND developer_id = #{id} AND project_id = #{record.id}" end ROR Lab.
    62. 62. - special options for HABTM - :insert_sql class Developer < ActiveRecord::Base has_and_belongs_to_many :active_projects, :join_table => developers_projects, :insert_sql => "INSERT INTO developers_projects VALUES(#{id},#{record.id})" end ROR Lab.
    63. 63. - special options for HABTM - :join_table class Category < ActiveRecord::Base   has_and_belongs_to_many :products, :join_table => “cats_prods” end   class Product < ActiveRecord::Base   has_and_belongs_to_many :categories, :join_table => “cats_prods” default join table => “categories_products” ROR Lab.
    64. 64. Association Callbacks• in the lifecycle of a collection • before_add • after_add • before_remove • after_remove ROR Lab.
    65. 65. Association Callbacksclass Customer < ActiveRecord::Base  has_many :orders, :before_add => :check_credit_limit   def check_credit_limit(order)    ...  endendclass Customer < ActiveRecord::Base  has_many :orders,    :before_add => [:check_credit_limit, :calculate_shipping_charges]   def check_credit_limit(order)    ...  end   def calculate_shipping_charges(order)    ...  end ROR Lab.
    66. 66. Summary• http://jonathanhui.com/ruby-rails-3-model- association ROR Lab.
    67. 67. 감사합니다.
    68. 68.   ROR Lab.

    ×