dump_serialize_table.rb 953 B

1234567891011121314151617181920212223242526272829303132333435
  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 'rubygems'
  12. require 'active_record'
  13. require 'pp'
  14. ActiveRecord::Schema.define do
  15. create_table :purchases, :force => true do |t|
  16. t.string :name
  17. t.text :last_five
  18. end
  19. end
  20. class Purchase < ActiveRecord::Base
  21. serialize :last_five
  22. # ...
  23. end
  24. purchase = Purchase.new
  25. purchase.name = "Dave Thomas"
  26. purchase.last_five = [ 'shoes', 'shirt', 'socks', 'ski mask', 'shorts' ]
  27. purchase.save
  28. purchase = Purchase.find_by_name("Dave Thomas")
  29. pp purchase.last_five
  30. pp purchase.last_five[3]