polymorphic.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. $: << File.dirname(__FILE__)
  10. require "connect"
  11. require "logger"
  12. #ActiveRecord::Base.logger = Logger.new(STDERR)
  13. require "rubygems"
  14. require "active_record"
  15. if ARGV.empty? or ARGV.first == '1'
  16. ActiveRecord::Schema.define do
  17. create_table :catalog_entries, :force => true do |t|
  18. t.string :name
  19. t.datetime :acquired_at
  20. t.integer :resource_id
  21. t.string :resource_type
  22. end
  23. create_table :articles, :force => true do |t|
  24. t.text :content
  25. end
  26. create_table :sounds, :force => true do |t|
  27. t.binary :content
  28. end
  29. create_table :images, :force => true do |t|
  30. t.binary :content
  31. end
  32. end
  33. end
  34. class CatalogEntry < ActiveRecord::Base
  35. belongs_to :resource, :polymorphic => true
  36. end
  37. class Article < ActiveRecord::Base
  38. has_one :catalog_entry, :as => :resource
  39. end
  40. class Sound < ActiveRecord::Base
  41. has_one :catalog_entry, :as => :resource
  42. end
  43. class Image < ActiveRecord::Base
  44. has_one :catalog_entry, :as => :resource
  45. end
  46. case ARGV.shift
  47. when "1"
  48. a = Article.new(:content => "This is my new article")
  49. c = CatalogEntry.new(:name => 'Article One', :acquired_at => Time.now)
  50. c.resource = a
  51. c.save!
  52. article = Article.find(1)
  53. p article.catalog_entry.name #=> "Article One"
  54. cat = CatalogEntry.find(1)
  55. resource = cat.resource
  56. p resource #=> #<Article:0x640d80 @attributes={"id"=>"1",
  57. # "content"=>"This is my new article"}>
  58. when "2"
  59. c = CatalogEntry.new(:name => 'Article One', :acquired_at => Time.now)
  60. c.resource = Article.new(:content => "This is my new article")
  61. c.save!
  62. c = CatalogEntry.new(:name => 'Image One', :acquired_at => Time.now)
  63. c.resource = Image.new(:content => "some binary data")
  64. c.save!
  65. c = CatalogEntry.new(:name => 'Sound One', :acquired_at => Time.now)
  66. c.resource = Sound.new(:content => "more binary data")
  67. c.save!
  68. CatalogEntry.find(:all).each do |c|
  69. puts "#{c.name}: #{c.resource.class}"
  70. end
  71. else
  72. a = Sound.new(:content => "ding!")
  73. c = CatalogEntry.new(:name => 'Sound One', :acquired_at => Time.now)
  74. c.resource = a
  75. c.save!
  76. c = CatalogEntry.find 1
  77. p c.resource
  78. a = Sound.find :first
  79. p a.catalog_entry
  80. end