質問編集履歴

1

post.rb, user.rb と schemaの追記

2023/05/11 06:19

投稿

yoshi32
yoshi32

スコア2

test CHANGED
File without changes
test CHANGED
@@ -94,6 +94,72 @@
94
94
  end
95
95
  ```
96
96
 
97
+ ```post.rb
98
+ class Post < ApplicationRecord
99
+ validates :title, presence: true, length: { maximum: 30 }
100
+ validates :content, presence: true, length: { maximum: 100}
101
+
102
+ belongs_to :user
103
+ def user
104
+ return User.find_by(id: self.user_id)
105
+ end
106
+ end
107
+ ```
108
+
109
+ ```user.rb
110
+ class User < ApplicationRecord
111
+ authenticates_with_sorcery!
112
+
113
+ validates :password, length: { minimum: 3 }, if: -> { new_record? || changes[:crypted_password] }
114
+ validates :password, confirmation: true, if: -> { new_record? || changes[:crypted_password] }
115
+ validates :password_confirmation, presence: true, if: -> { new_record? || changes[:crypted_password] }
116
+
117
+ validates :email, uniqueness: true
118
+ validates :email, presence: true
119
+ validates :first_name, presence: true, length: { maximum: 255 }
120
+ validates :last_name, presence: true, length: { maximum: 255 }
121
+
122
+ has_many :posts, dependent: :destroy, foreign_key: :post_user_id
123
+ end
124
+ ```
125
+
126
+ ```schema
127
+ # This file is auto-generated from the current state of the database. Instead
128
+ # of editing this file, please use the migrations feature of Active Record to
129
+ # incrementally modify your database, and then regenerate this schema definition.
130
+ #
131
+ # Note that this schema.rb definition is the authoritative source for your
132
+ # database schema. If you need to create the application database on another
133
+ # system, you should be using db:schema:load, not running all the migrations
134
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
135
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
136
+ #
137
+ # It's strongly recommended that you check this file into your version control system.
138
+
139
+ ActiveRecord::Schema.define(version: 2023_05_07_041659) do
140
+
141
+ create_table "posts", force: :cascade do |t|
142
+ t.string "title"
143
+ t.text "content"
144
+ t.datetime "created_at", null: false
145
+ t.datetime "updated_at", null: false
146
+ end
147
+
148
+ create_table "users", force: :cascade do |t|
149
+ t.string "email", null: false
150
+ t.string "crypted_password"
151
+ t.string "salt"
152
+ t.string "first_name", null: false
153
+ t.string "last_name", null: false
154
+ t.datetime "created_at", null: false
155
+ t.datetime "updated_at", null: false
156
+ t.index ["email"], name: "index_users_on_email", unique: true
157
+ end
158
+
159
+ end
160
+
161
+ ```
162
+
97
163
  ### 試したこと
98
164
  下記内容が原因ではないかとコードの一部修正を行ってみましたが、エラーは解消されませんでした。
99
165
  ・current_userがnilである場合に備えて、デフォルト値を設定する
@@ -169,6 +235,7 @@
169
235
  end
170
236
 
171
237
  ```
238
+
172
239
 
173
240
  ### 補足情報(FW/ツールのバージョンなど)
174
241