在我的网络应用程序中,我正在构建记笔记功能。我希望某些学生能有笔记,我希望老师能够写下关于学生的笔记。所以我希望能够将笔记与学生联系起来。我怎么能做这样的事情?
显而易见的解决方案是制作Notes(belongs_to学生)和学生(has_many笔记)。我认为这是方法,但我不确定。唯一的问题是我负责Note。其他人负责学生,会议等。
问题:我负责笔记,而其他人则负责学生,会议等。
我能做什么,我应该做什么研究。任何帮助,将不胜感激。 Schema.rb
create_table "session_notes", force: :cascade do |t|
t.text "note"
t.integer "session_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "sessions", force: :cascade do |t|
t.datetime "start_time"
t.datetime "end_time"
t.integer "session_teacher"
t.integer "session_student"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "students", force: :cascade do |t|
t.string "full_name"
t.string "screen_name"
t.string "icon"
t.string "color"
t.string "contact_info"
t.text "description"
t.integer "session_interval"
t.integer "school_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "teachers", force: :cascade do |t|
t.string "user_name"
t.string "password_digest"
t.datetime "last_login"
t.string "full_name"
t.string "screen_name"
t.string "icon"
t.string "color"
t.string "email"
t.text "description"
t.string "powers"
t.integer "school_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Sessions_note_controller
class SessionNotesController < ApplicationController
#before_action :set_session_note, only: [:show, :edit, :update, :destroy]
# GET /session_notes
# GET /session_notes.json
def index
@session_note = SessionNote.new
@session_notes = SessionNote.paginate(page: params[:page])
end
# GET /session_notes/1
# GET /session_notes/1.json
def show
end
# GET /session_notes/new
def new
@session_note = SessionNote.new
end
# GET /session_notes/1/edit
def edit
end
# POST /session_notes
# POST /session_notes.json
def create
@session_note = SessionNote.new(session_note_params)
respond_to do |format|
if @session_note.save
format.html { redirect_to session_notes_url, notice: 'Session note was successfully created.' }
format.json { render :index, status: :created, location: @session_note }
else
format.html { render :new }
format.json { render json: @session_note.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /session_notes/1
# PATCH/PUT /session_notes/1.json
def update
respond_to do |format|
if @session_note.update(session_note_params)
format.html { redirect_to @session_note, notice: 'Session note was successfully updated.' }
format.json { render :show, status: :ok, location: @session_note }
else
format.html { render :edit }
format.json { render json: @session_note.errors, status: :unprocessable_entity }
end
end
end
# DELETE /session_notes/1
# DELETE /session_notes/1.json
def destroy
@session_note.destroy
respond_to do |format|
format.html { redirect_to session_notes_url, notice: 'Session note was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# # Use callbacks to share common setup or constraints between actions.
# def set_session_note
# @session_note = SessionNote.find(params[:id])
# end
# Never trust parameters from the scary internet, only allow the white list through.
def session_note_params
params.require(:session_note).permit(:note, :session_id, :created_at)
end
end
Sessions_controller
class SessionsController < ApplicationController
before_action :set_session, only: [:show, :edit, :update, :destroy]
# GET /sessions
# GET /sessions.json
def index
@sessions = Session.all
end
# GET /sessions/1
# GET /sessions/1.json
def show
@student = Student.find(@session.session_student)
@teacher = Teacher.find(@session.session_teacher)
end
# GET /sessions/new
def new
@session = Session.new
end
# GET /sessions/1/edit
def edit
end
# POST /sessions
# POST /sessions.json
def create
@session = Session.new(session_params)
respond_to do |format|
if @session.save
format.html { redirect_to @session, notice: 'Session was successfully created.' }
format.json { render :show, status: :created, location: @session }
else
format.html { render :new }
format.json { render json: @session.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /sessions/1
# PATCH/PUT /sessions/1.json
def update
respond_to do |format|
if @session.update(session_params)
format.html { redirect_to @session, notice: 'Session was successfully updated.' }
format.json { render :show, status: :ok, location: @session }
else
format.html { render :edit }
format.json { render json: @session.errors, status: :unprocessable_entity }
end
end
end
# DELETE /sessions/1
# DELETE /sessions/1.json
def destroy
@session.destroy
respond_to do |format|
format.html { redirect_to sessions_url, notice: 'Session was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_session
@session = Session.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def session_params
params.require(:session).permit(:start_time, :end_time, :session_teacher, :session_student)
end
end
答案 0 :(得分:1)
你需要的是什么
您的students
和session_notes
表
Student
实例has_many session_notes SessionNote
实例belongs_to student teachers
和session_notes
表
Teacher
实例has_many session_notes SessionNote
实例belongs_to老师生成迁移以在student_id
表
session_notes
rails g migration add_student_id_to_session_notes student_id:integer
生成迁移以在teacher_id
表
session_notes
rails g migration add_teacher_id_to_session_notes teacher_id:integer
然后运行迁移bundle exec rake db:migrate
# app/models/session_note.rb
class SessionNote < ActiveRecord::Base
belongs_to :student
belongs_to :teacher
end
# app/models/student.rb
class Student < ActiveRecord::Base
has_many :session_notes
end
# app/models/teacher.rb
class Teacher < ActiveRecord::Base
has_many :session_notes
end
使用它们:
Student.last.session_notes
Teacher.last.session_notes
答案 1 :(得分:0)
我在您的帖子中挑选问题时遇到了问题。您是否可以尝试更具体地了解您的目标是什么或您不理解的内容? 那就是说......
如果我理解正确,你想建立一个has_many
类型的关系,但你还不确定如何做到这一点。如果是这种情况,请查看rails docs的这一部分,以便将一个模型与另一个模型相关联。
基本上,通过我上面看到的代码,您可以设置一个新模型并将其放在<your app's root directory>/app/models/
目录中。该类有几行可能类似于
class SessionNote < ApplicationRecord
belongs_to :Teacher
# code that does things you need your model to do
end
等等。 我甚至不确定我是否在正确的轨道上有答案所以我会发布这个并让你更新一些细节。 如果您对我的意思有任何疑问,或者我没有向您提供您想要的信息,请告诉我。
答案 2 :(得分:0)
从你问题的性质来看,我会假设你对软件很陌生。
您负责Notes,但注意功能可以影响并依赖于其他功能。
不确定为什么要显示sessions_controller,但我很高兴你做到了。我现在意识到学生和老师之间的Session
是多对多的。完全了解了软件中会话的含义。
因此,查看您的架构文件,将会话与笔记相关联。
会话代表教师和学生的实例。到目前为止一切都很好。
因为会话属于教师和学生,我们可以利用它来表达相同的笔记,同时说一个笔记属于特定的会话。
我想你真正想知道的是如何编码它。因为您只添加了样板控制器代码并且根本没有提供任何型号代码,所以经典:)
class SessionNote < ActiveRecord::Base # ApplicationRecord for Rails 5+
belongs_to :session
delegate :teacher, :student, to: :session
end
class Session < ActiveRecord::Base
belongs_to :teacher, foreign_key: 'teacher_session'
belongs_to :student, foreign_key: 'student_session'
has_many :session_notes
end
class Student < ActiveRecord::Base
has_many :sessions
has_many :session_notes, through: :sessions
end
class Teacher < ActiveRecord::Base
has_many :sessions
has_many :session_notes, through: :sessions
end