$30 off During Our Annual Pro Sale. View Details »

Rails Workshop 2

Mike Skalnik
February 27, 2012

Rails Workshop 2

Mike Skalnik

February 27, 2012
Tweet

More Decks by Mike Skalnik

Other Decks in Programming

Transcript

  1. Ruby Rails ON 2 Monday, February 27, 12

  2. About Me Mike Skalnik github.com/skalnik @skalnik Monday, February 27, 12

  3. Where did we stop? We have posts! Monday, February 27,

    12
  4. ActiveRecord Associations Monday, February 27, 12

  5. 4 Kinds Monday, February 27, 12 has_one, has_many, habtm/has_many :through,

    belongs_to
  6. has_one Georgia Tech A School Buzz Mascot has_one Monday, February

    27, 12
  7. has_one class School < ActiveRecord::Base has_one :mascot end Monday, February

    27, 12
  8. has_many id title body 1 First Post! … 2 The

    Value of… … id author body post_id 1 Mike … 1 2 Eric … 1 3 Kyle … 2 Monday, February 27, 12
  9. has_many class Post < ActiveRecord::Base has_many :comments end Monday, February

    27, 12
  10. belongs_to class Comment < ActiveRecord::Base belongs_to :post end Monday, February

    27, 12
  11. belongs_to class Mascot < ActiveRecord::Base belongs_to :school end Monday, February

    27, 12
  12. has_many :through has_and_belongs_to vs Monday, February 27, 12

  13. expose join table hide join table vs Monday, February 27,

    12 Exposed as a model
  14. class Category < ActiveRecord::Base has_many :categorizations has_many :posts, :through =>

    categorizations end class Categorization < ActiveRecord::Base belongs_to :post belongs_to :category end class Post < ActiveRecord::Base has_many :categorizations has_many :categories, :through => categorizations end Monday, February 27, 12 Allows for validations, along with attaching data to the association. How about Users with Ratings & Songs?
  15. vs Monday, February 27, 12

  16. class Post < ActiveRecord::Base has_and_belongs_to_many :categories end class Category <

    ActiveRecord::Base has_and_belongs_to_many :posts end Monday, February 27, 12 No touching the table, just a link. Still need to write migration though!
  17. Usage @post.comments #=> [#<Comment>, …] @post.categories #=> [#<Category>, …] @comment.post

    #=> #<Post> @school.mascot #=> #<Mascot> Monday, February 27, 12
  18. $ terminal_time Monday, February 27, 12

  19. ActiveRecord Validations ✖ ✓ Monday, February 27, 12 Not a

    full list, but the more common ones
  20. validates :terms, :acceptance => true Monday, February 27, 12 Accept

    TOS
  21. validates :password, :confirmation => true Monday, February 27, 12 confirm

    password
  22. validates :username, :exclusion => { :in => [‘user’] } Monday,

    February 27, 12 reserved usernames
  23. validates :email, :format => { :with => /.*@.*/ } Monday,

    February 27, 12 validate email format
  24. validates :size, :inclusion => { :in => [‘small’, …] }

    Monday, February 27, 12 size is in list
  25. validates :name, :length => { :minimum => 2, :maximum =>

    500, :in => 2..500, :is => 12 } Monday, February 27, 12 Lots of options
  26. validates :points, :numericality => true Monday, February 27, 12

  27. validates :email, :presence => true Monday, February 27, 12 Ensure

    it’s there
  28. validates :email, :uniqueness => true Monday, February 27, 12 Ensure

    they haven’t registered before
  29. Usage @post.save #=> false if not valid @post.valid? Monday, February

    27, 12
  30. More Resources Monday, February 27, 12 Not a full list,

    but the more common ones
  31. RailsGuides http://guides.rubyonrails.org Monday, February 27, 12 Not a full list,

    but the more common ones
  32. RailsCasts http://railscasts.com Monday, February 27, 12 Not a full list,

    but the more common ones
  33. Ruby Toolbox http://ruby-toolbox.com Monday, February 27, 12 Not a full

    list, but the more common ones
  34. Me hi@mikeskalnik.com Monday, February 27, 12 Not a full list,

    but the more common ones
  35. @ Web Tech Innovators Monday, February 27, 12

  36. ATLRUG Monday, February 27, 12

  37. THANKS Monday, February 27, 12