Guest User

Untitled

a guest
Jul 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. # app/models/post.rb
  2. class Post < ActiveRecord::Base
  3. # ASSOCIATIONS
  4. belongs_to :postable, :polymorphic => true, :counter_cache => true
  5. end
  6.  
  7. # app/models/destination.rb
  8. class Destination < ActiveRecord::Base
  9. # ASSOCIATIONS
  10. has_many :posts, :as => :postable, :dependent => :delete_all
  11. end
  12.  
  13. # migration
  14. class CreatePosts < ActiveRecord::Migration
  15. def self.up
  16. create_table :posts do |t|
  17. t.string :title
  18. t.string :permalink
  19. t.text :content
  20. t.datetime :activated_at
  21. t.references :postable, :polymorphic => true # campo polimórfico
  22. t.references :user
  23.  
  24. t.timestamps
  25. end
  26. add_index :posts, :permalink
  27. end
  28.  
  29. def self.down
  30. remove_index :posts, :permalink
  31. drop_table :posts
  32. end
  33. end
Add Comment
Please, Sign In to add comment