1
0
Fork 0

Add model Post

This commit is contained in:
Alex Kotov 2021-03-10 14:31:29 +05:00
parent 4883b21b30
commit b86d78fac0
6 changed files with 41 additions and 1 deletions

5
app/models/post.rb Normal file
View File

@ -0,0 +1,5 @@
class Post < ApplicationRecord
belongs_to :profile
validates :text, length: { maximum: 200 }
end

View File

@ -1,2 +1,3 @@
class Profile < ApplicationRecord class Profile < ApplicationRecord
has_many :posts
end end

View File

@ -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

11
db/schema.rb generated
View File

@ -10,7 +10,15 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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| create_table "profiles", force: :cascade do |t|
t.datetime "created_at", precision: 6, null: false t.datetime "created_at", precision: 6, null: false
@ -20,4 +28,5 @@ ActiveRecord::Schema.define(version: 2021_03_10_092513) do
t.string "description" t.string "description"
end end
add_foreign_key "posts", "profiles"
end end

9
test/fixtures/posts.yml vendored Normal file
View File

@ -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!

7
test/models/post_test.rb Normal file
View File

@ -0,0 +1,7 @@
require "test_helper"
class PostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end