【发布时间】:2017-09-18 11:54:31
【问题描述】:
条件:
导师可以关注或不关注学生
学生可以关注或不关注导师
Tutor 和 Student 是两种不同的数据模型。
我建立了一个名为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
应用程序.rb
class Application < ActiveRecord::Base
belongs_to :tutor
belongs_to :student
end
学生.rb
has_many :applications, :dependent => :destroy
has_many :tutors, through: :applications
导师.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
class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception include SessionsHelper include SecondsessionsHelper endnothing inside -
请编辑您的问题并在此处添加代码,而不是在 cmets 中显示。
标签: html ruby-on-rails ruby ruby-on-rails-4 associations