6. Hands On Vamos ver como é isso. HANDS ON http://www.flickr.com/photos/jurvetson/489257240/
7. rails g model review review_text:text t.references :reviewable , :polymorphic => true
8. Ali! No teto! Polymorphic!!!! Me enganei. É só um PomboRanger.
9. class Review < ActiveRecord::Base belongs_to :reviewable , :polymorphic => true end module Reviewable def is_reviewable has_many :reviews , :as => :reviewable , :dependent => :destroy include InstanceMethods end module InstanceMethods def reviewable? true end end end ActiveRecord::Base.extend Reviewable
10. class Produto < ActiveRecord::Base is_reviewable end class Categoria < ActiveRecord::Base is_reviewable end class Artigo < ActiveRecord::Base is_reviewable end
Apresentação tomando como base explicação em artigo de cowboycoded
Criar um 'plugin' que permita fazer reviews de um produto ou qualquer outra coisa
Polymorphic relationships allow you to have a single model that can be associated to an arbitrary number of other model types. Example For instance, let's say you have a contacts database utilizing two models, Person and Company. Both people and companies have addresses that you want to store in the database, and you want to be able to query against all addresses at once (say for a location-based search). Polymorphic associations make this easy in Rails. Let's assume that you already have your Person and Company models created and are just creating your Address model. When you create your migration you will denote the polymorphic relationship using references: http://wiki.rubyonrails.org/howtos/db-relationships/polymorphic