From b86d78fac0be6a2807d84ac61c4b1fe2b4e868e8 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 10 Mar 2021 14:31:29 +0500 Subject: [PATCH] Add model Post --- app/models/post.rb | 5 +++++ app/models/profile.rb | 1 + db/migrate/20210310092955_create_posts.rb | 9 +++++++++ db/schema.rb | 11 ++++++++++- test/fixtures/posts.yml | 9 +++++++++ test/models/post_test.rb | 7 +++++++ 6 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 app/models/post.rb create mode 100644 db/migrate/20210310092955_create_posts.rb create mode 100644 test/fixtures/posts.yml create mode 100644 test/models/post_test.rb diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..7dfe368 --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,5 @@ +class Post < ApplicationRecord + belongs_to :profile + + validates :text, length: { maximum: 200 } +end diff --git a/app/models/profile.rb b/app/models/profile.rb index 8b5c14e..2107e8c 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -1,2 +1,3 @@ class Profile < ApplicationRecord + has_many :posts end diff --git a/db/migrate/20210310092955_create_posts.rb b/db/migrate/20210310092955_create_posts.rb new file mode 100644 index 0000000..0b7654c --- /dev/null +++ b/db/migrate/20210310092955_create_posts.rb @@ -0,0 +1,9 @@ +class CreatePosts < ActiveRecord::Migration[6.1] + def change + create_table :posts do |t| + t.timestamps + t.references :profile, null: false, foreign_key: true + t.string :text, null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 3b50af6..9664d07 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_03_10_092513) do +ActiveRecord::Schema.define(version: 2021_03_10_092955) do + + create_table "posts", force: :cascade do |t| + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.integer "profile_id", null: false + t.string "text", null: false + t.index ["profile_id"], name: "index_posts_on_profile_id" + end create_table "profiles", force: :cascade do |t| t.datetime "created_at", precision: 6, null: false @@ -20,4 +28,5 @@ ActiveRecord::Schema.define(version: 2021_03_10_092513) do t.string "description" end + add_foreign_key "posts", "profiles" end diff --git a/test/fixtures/posts.yml b/test/fixtures/posts.yml new file mode 100644 index 0000000..70b0219 --- /dev/null +++ b/test/fixtures/posts.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + profile: one + text: Hello, World! + +two: + profile: two + text: OMG this is a real microblog! diff --git a/test/models/post_test.rb b/test/models/post_test.rb new file mode 100644 index 0000000..ff155c4 --- /dev/null +++ b/test/models/post_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class PostTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end