这是我的rails应用程序中的关系:
school.rb :
class School < ActiveRecord::Base
has_many :teachers, dependent: :destroy
has_many :ranks, dependent: :destroy
end
teacher.rb :
class Teacher < ActiveRecord::Base
belongs_to :school
has_many :ratings
end
rating.rb :
class Rating < ActiveRecord::Base
belongs_to :teacher
belongs_to :user
end
当我尝试访问学校展示页面中的评分时,出现错误
未定义的方法`评级&#39;对于教师
我真的不太明白这里的错误。它是说我无法访问评级方法,因为我在学校和评级之间没有任何关系吗?我想在我的学校展示页面上输出平均评分最高的3位最佳教师。我被卡在这里了。任何帮助,将不胜感激。感谢您查看我的代码。
学校/ show.haml :
.text-center
%span.website
%h1.school_name= @school.name
%em.website= link_to('Website', @school.website, target: '_blank')
%hr
TOP Giảng viên:
= @teachers.ratings.size
schools_controller.rb :
class SchoolsController < ApplicationController
before_action :set_school, only: [:show, :edit, :update, :destroy]
def index
@schools = School.text_search(params[:query])
end
def show
@teachers = @school.teachers.all
end
# GET /schools/new
def new
@school = School.new
end
# GET /schools/1/edit
def edit
end
# POST /schools
# POST /schools.json
def create
@school = School.new(school_params)
respond_to do |format|
if @school.save
format.html { redirect_to @school, notice: 'School was successfully created.' }
format.json { render :show, status: :created, location: @school }
else
format.html { render :new }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /schools/1
# PATCH/PUT /schools/1.json
def update
respond_to do |format|
if @school.update(school_params)
format.html { redirect_to @school, notice: 'School was successfully updated.' }
format.json { render :show, status: :ok, location: @school }
else
format.html { render :edit }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
# DELETE /schools/1
# DELETE /schools/1.json
def destroy
@school.destroy
respond_to do |format|
format.html { redirect_to schools_url, notice: 'School was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_school
@school = School.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def school_params
params.require(:school).permit(:name, :website)
end
end
schema.rb :
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160222173653) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "pg_search_documents", force: :cascade do |t|
t.text "content"
t.integer "searchable_id"
t.string "searchable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "pg_search_documents", ["searchable_type", "searchable_id"], name: "index_pg_search_documents_on_searchable_type_and_searchable_id", using: :btree
create_table "ranks", force: :cascade do |t|
t.integer "internet"
t.integer "food"
t.integer "library"
t.integer "club"
t.integer "campus"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "school_id"
t.text "comment"
t.integer "opportunity"
t.integer "happiness"
t.integer "social"
end
create_table "ratings", force: :cascade do |t|
t.integer "clarity"
t.integer "helpfulness"
t.integer "easiness"
t.text "comment"
t.integer "teacher_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "schoolYear"
t.string "gradeReceived"
t.string "subject"
end
add_index "ratings", ["teacher_id"], name: "index_ratings_on_teacher_id", using: :btree
create_table "schools", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "website"
end
create_table "teachers", force: :cascade do |t|
t.string "firstName"
t.string "lastName"
t.string "middleName"
t.integer "school_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "department"
t.string "full_name"
end
add_index "teachers", ["school_id"], name: "index_teachers_on_school_id", using: :btree
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_foreign_key "ratings", "teachers"
add_foreign_key "teachers", "schools"
end
答案 0 :(得分:1)
@teachers = @school.teachers.all
- 返回属于该学校的所有教师。
在展示中,您无法访问@teachers.ratings.size
之类的评分,您应该遍历每位教师并获得如下所示的尺寸:
@teachers.each do |t|
# your can use ratings of a teacher|t|
end