掲示板アプリ開発を行なっており、
やりたかったこととしては、
①親ユーザー(1)と子ユーザー(多数)がいる
②子ユーザーからコメントがあった場合、親ユーザーがコメントが合った順に見られる
ここまで実装確認。
③親ユーザーが見やすいようにコメントをハイライト
④seedファイルにテストデーターを作成して反映 <ー エラー発生
最短距離でしっかり身に付く! Webアプリケーション開発の教科書
~Ruby on Railsで作る本格Webアプリ~ という本を参考に行なっています。
エラー内容
PG::UndefinedColumn: ERROR: column "updated" does not exist LINE 1: ...users"."role" = $1 AND "users"."id" = $2 ORDER BY updated de... ^ HINT: Perhaps you meant to reference the column "users.updated_at".
controller
1class UsersController < ApplicationController 2 before_action :authenticate_user! 3 4 def show 5 @user = User.find(params[:id]) 6 # @students = User.where(role: 'student').order('updated_at desc') if @user.role == "teacher" 7 @posts = Post.where(user_id: @user.id).order("created_at desc") 8 9 visit = current_user.visits.find_by(visitee: @user.id) 10 if (visit != nil) 11 visit.touch 12 else 13 current_user.visits << Visit.create(visitee: @user.id) 14 end 15 16 if @user.role == "teacher" 17 @students = User.where(role: "student").order("updated desc") 18 @updated_users = [] 19 current_user.visits.each {|v| 20 st = @students.find_by(id: v.visitee) 21 next if st == nil 22 @updated_users.push(st.id) if (Post.where(user_id: v.visitee).where(updated_at: v.updated_at ... Time.now).size > 0)} 23 end 24 end 25end 26 27
seed.rb
1names = ["Mr. Teacher","Antonio Banderas","Cynthia Erivo", 2"Scarlett Johansson","Jonathan Pryce","Florence Pugh", 3"Yalitza Aparicio","Olivia Colman","Marina de Tavira", 4"Adam Driver","Sam Elliott","Lady Gaga","Richard E. Grant", 5"Regina King","Rami Malek","Mary J. Blige","Timothee Chalamet", 6"Allison Janney","Daniel Kaluuya","Lesley Manville", 7"Laurie Metcalf","Margot Robbie","Sam Rockwell","Mahershala Ali"] 8 9names.each_with_index {|name, i| 10 uid = sprintf "user%03d", i 11 User.create(username: uid, fullname: name, 12 password: "123456", 13 role: ((i == 0) ? "teacher" : "student")) 14 Visit.create(user_id: 1, visitee: i) 15 16} 17
viewファイル
1<h1>My page for <%= @user.fullname %> </h1> 2 3<% if @students != nil %> 4 <p> 5 <% @students.each {|st| 6 color = @updated_users.include?(st.id) ? "btn btn-warning" : "btn btn-light" %> 7 <%= link_to st.fullname, user_path(st.id), class: "color" %> 8 <% } %> 9 </p> 10<% end %> 11 12<div class="form-group"> 13 <%= form_with model: Post.new do |f| %> 14 <%= f.text_area :body, placeholder: "Add your coment", class:"form-control", rows: 10 %> 15 <%= f.hidden_field :id, { value: @user.id } %> 16 <%= f.submit "submit" ,class: "btn btn-primary "%> 17 <% end %> 18</div> 19 20<% if @posts != nil %> 21 <% l = @posts.length; @posts.each_with_index {|post, i| 22 submitter = User.find_by(id: post.submitter) 23 sname = (submitter != nil) ? submitter.fullname : "Anonymous" %> 24 <div class="card"> 25 <div class="card-header"> 26 <%= (l-i).to_s + " : " %> 27 <%= (submitter !=nil) ? (link_to sname, user_path(submitter)) : sname %> 28 <%= post.created_at.to_s %></div> 29 <div class= "card-body"> 30 <%= simple_format(h(post.body)) %></div> 31 </div> 32 <% } %> 33<% end %>
create_table "posts", force: :cascade do |t| t.integer "user_id" t.text "body" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "submitter" end
とりあえずカラムがない?のが原因と考え、Seedファイルの内容を再度確認、dbを再作成を行いましたが、エラーが出たままです。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー