Skip to content

Commit

Permalink
added database relationship topic and blog
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyB00y committed Jun 19, 2017
1 parent 1daf1e4 commit a28d6a9
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/models/blog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ class Blog < ApplicationRecord
enum status: { draft: 0, published: 1 }
extend FriendlyId
friendly_id :title, use: :slugged
belongs_to :topic
end
4 changes: 4 additions & 0 deletions app/models/topic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Topic < ApplicationRecord
validates_presence_of :title
has_many :blogs
end
11 changes: 6 additions & 5 deletions app/views/blogs/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th colspan="3"></th>
<th>Blog Title</th>
<th>Body</th>
<th colspan="4"></th>
</tr>
</thead>

Expand All @@ -16,14 +16,15 @@
<tr>
<td><%= blog.title %></td>
<td><%= blog.description %></td>
<td><%= link_to blog.status, toggle_status_blog_path(blog) %></td>
<td><%= link_to 'Show', blog %></td>
<td><%= link_to 'Edit', edit_blog_path(blog) %></td>
<td><%= link_to 'Destroy', blog, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to 'Delete Post', blog, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Blog', new_blog_path %>
<%= link_to 'New Blog', new_blog_path %>
9 changes: 9 additions & 0 deletions db/migrate/20170619162842_create_topics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateTopics < ActiveRecord::Migration[5.1]
def change
create_table :topics do |t|
t.string :title

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20170619180451_add_topic_reference_to_blogs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTopicReferenceToBlogs < ActiveRecord::Migration[5.1]
def change
add_column :blogs, :topic_references, :string
end
end
8 changes: 7 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170619154324) do
ActiveRecord::Schema.define(version: 20170619162842) do

create_table "blogs", force: :cascade do |t|
t.string "title"
Expand Down Expand Up @@ -51,4 +51,10 @@
t.datetime "updated_at", null: false
end

create_table "topics", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
7 changes: 7 additions & 0 deletions test/fixtures/topics.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyString

two:
title: MyString
7 changes: 7 additions & 0 deletions test/models/topic_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class TopicTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit a28d6a9

Please sign in to comment.