Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. 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
  2. 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?
  3. 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!
  4. Usage @post.comments #=> [#<Comment>, …] @post.categories #=> [#<Category>, …] @comment.post

    #=> #<Post> @school.mascot #=> #<Mascot> Monday, February 27, 12
  5. validates :email, :format => { :with => /.*@.*/ } Monday,

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

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

    500, :in => 2..500, :is => 12 } Monday, February 27, 12 Lots of options