Final Fantasy VIII
Final Fantasy VIII

Reputation: 67

Can't access ratings in my school show page

here are the relationships in my rails application:

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

When I try to access the ratings in my school show page, I get the error

undefined method `ratings' for Teachers

I don't really quite understand the error here. Is it saying that I can't access to the ratings method because I dont have any relationship between school and ratings? I am trying to output best 3 teachers who have the highest average ratings on my school show page. I am getting stuck here. Any help would be appreciated. Thank you for looking at my code.

schools/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

Upvotes: 0

Views: 28

Answers (1)

Gokul
Gokul

Reputation: 3231

@teachers = @school.teachers.all - returns all teachers belong to that school.

In show you cannot access ratings like @teachers.ratings.size instead, you should iterate through each teacher and get the size as below:

@teachers.each do |t|
    # your can use ratings of a teacher|t|
end

Upvotes: 1

Related Questions