導師可以按照或DIS遵循學生
學生可以按照或DIS跟隨導師
,教師和學生是兩個不同的數據模型。
我建立名爲application
中間數據模型連接兩個模型
應用模型:
class CreateApplications < ActiveRecord::Migration
def change
create_table :applications do |t|
t.belongs_to :tutor, index: true
t.belongs_to :student, index: true
t.timestamps null: false
end
end
end
application.rb中
class Application < ActiveRecord::Base
belongs_to :tutor
belongs_to :student
end
student.rb
has_many :applications, :dependent => :destroy
has_many :tutors, through: :applications
tutor.rb
has_many :applications, :dependent => :destroy
has_many :students, through: :applications
schema.rb
ActiveRecord::Schema.define(version: 20170421093747) do
create_table "applications", force: :cascade do |t|
t.integer "tutor_id"
t.integer "student_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "applications", ["student_id"], name: "index_applications_on_student_id"
add_index "applications", ["tutor_id"], name: "index_applications_on_tutor_id".......
show.html.erb
# -->show who is the follower & person who followed & How many of them
# Error :undefined method `student' for nil:NilClass
<%= @application.student.count %> #undefined method `student' for nil:NilClass
<%= @application.tutor.count % >#undefined method `student' for nil:NilClass
<%= @application.student%> #undefined method `student' for nil:NilClass
您的@application變量爲零。你可以在相關的控制器代碼的位置發佈信息嗎? – mysmallidea
@mysmallidea class ApplicationController
evalwt
請編輯您的問題,並添加代碼存在而不是顯示在評論它。 – Gerry