Skip to content

Instantly share code, notes, and snippets.

@crashtech
Created August 21, 2020 16:21
Show Gist options
  • Save crashtech/3712509fc4046da794c4ff53b0d8c815 to your computer and use it in GitHub Desktop.
Save crashtech/3712509fc4046da794c4ff53b0d8c815 to your computer and use it in GitHub Desktop.
# app/models/topic.rb
class Topic < ApplicationRecord
self.table_name = 'topic'
## Associations
has_many :posts
## Validations
validates :name, presence: true
end
# app/models/post.rb
class Post < ApplicationRecord
self.table_name = 'post'
## Associations
belongs_to :topic
## Validations
validates :title, presence: true
validates :text, presence: true
end
# db/schema.rb
ActiveRecord::Schema.define(version: 1) do
create_table "topic", force: :cascade do |t|
t.string "name", limit: 255, null: false
end
create_table "post", force: :cascade do |t|
t.bigint "topic_id", null: false
t.text "title", null: false
t.text "text", null: false
t.index ["topic_id"], name: "index_post_on_topic_id"
end
add_foreign_key "post", "topic"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment