Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require_relative "connection"
- ActiveRecord::Schema.define do
- drop_table :comments if ActiveRecord::Base.connection.data_source_exists? :comments
- drop_table :articles if ActiveRecord::Base.connection.data_source_exists? :articles
- create_table :articles do |table|
- table.string :title
- table.string :author
- end
- create_table :comments do |table|
- table.references :article, foreign_key: true
- table.text :content
- table.string :email
- end
- end
- class Article < ActiveRecord::Base
- has_many :comments
- before_destroy :clean_up_comments
- private
- def clean_up_comments
- comments.destroy_all
- end
- end
- class Comment < ActiveRecord::Base
- belongs_to :article
- end
- article = Article.create author: 'Mrs. Loveshercats', title: "My cats are angels"
- counterpoint = article.comments.create email: 'OhNo@You.Didnt', content: 'Your cats are abominable, hateful, spiteful, soulless poop demons.'
- article.destroy!
Add Comment
Please, Sign In to add comment