首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将多个笔记(数据模型)与学生关联(数据模型)

将多个笔记(数据模型)与学生关联(数据模型)
EN

Stack Overflow用户
提问于 2017-05-17 04:15:34
回答 3查看 70关注 0票数 0

在我的web应用程序中,我正在构建一个记录功能。我希望有一些学生的笔记出现,我希望老师能够写关于学生的笔记。所以我想把笔记和学生联系起来。我该怎么做这样的事?

最明显的解决办法是做一个笔记(belongs_to学生)和学生(has_many笔记)。我想是这样的,但我不确定。唯一的问题是我负责Note。其他人负责学生、课程等。

问题:我负责笔记,而其他人负责学生,会议等。

我能做什么,我应该研究什么。任何帮助都将不胜感激。Schema.rb

代码语言:javascript
复制
  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

代码语言:javascript
复制
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

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-05-17 05:10:01

您需要的是studentssession_notes表之间的一个多个关系。

  • 一个Student实例has_many session_notes
  • 一个SessionNote实例belongs_to学生

teacherssession_notes表之间的一对多关系

  • 一个Teacher实例has_many session_notes
  • 一个SessionNote实例belongs_to教师

生成迁移以在student_id表上添加session_notes

代码语言:javascript
复制
rails g migration add_student_id_to_session_notes student_id:integer

生成迁移以在teacher_id表上添加session_notes

代码语言:javascript
复制
rails g migration add_teacher_id_to_session_notes teacher_id:integer

然后运行迁移bundle exec rake db:migrate

代码语言:javascript
复制
# 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

使用它们:

代码语言:javascript
复制
Student.last.session_notes
Teacher.last.session_notes
票数 0
EN

Stack Overflow用户

发布于 2017-05-17 04:52:40

我很难在你的帖子里挑出一个问题。你能不能试着更具体地说明你的目标是什么,或者你不明白什么?

有人说..。

如果我的理解是正确的,你想要建立一个has_many类型的关系,但你还不知道如何去做。如果是这样的话,请看一看rails文档的这一段,它将一个模型与另一个模型关联起来。

基本上,使用上面看到的代码,您将设置一个新模型并将其放在<your app's root directory>/app/models/目录中。该类将有几行类似于

代码语言:javascript
复制
class SessionNote < ApplicationRecord
  belongs_to :Teacher

  # code that does things you need your model to do
end

诸若此类。我甚至不确定我是否在正确的轨道上的答案,所以我会张贴这个,让你更新与更多的细节。如果你对我的意思有任何疑问,或者我没有给你你想要的信息,请告诉我。

票数 0
EN

Stack Overflow用户

发布于 2017-05-17 05:09:12

从你问题的本质来看,我会假设你对软件很陌生。

您负责Notes,但是Notes功能可以影响并依赖于其他功能

不知道你为什么要给sessions_controller看,但我很高兴你给我看了。我现在意识到,在学生和老师之间,Session对很多人来说是一个很大的数目。完全理解了软件中会话的意义。

因此,看看您的模式文件,您将会话与注释关联起来。

会话表示教师和学生的一个实例。到目前一切尚好。

因为一节课属于老师和学生,我们可以利用它来表达同样的笔记,同时说一个笔记属于一个特定的会话。

我想你真正想知道的是如何编码它。因为您只添加了样板控制器代码,而且根本没有提供任何模型代码,经典:)

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44015437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档