【问题标题】:How to build M to M relationships between two data model如何在两个数据模型之间建立 M 到 M 关系
【发布时间】: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 &lt; 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 end nothing inside
  • 请编辑您的问题并在此处添加代码,而不是在 cmets 中显示。

标签: html ruby-on-rails ruby ruby-on-rails-4 associations


【解决方案1】:

当模型与其他模型有很多关系时,你想使用复数名称,即tutor.students或student.tutors

UPD:另外,您没有正确地将变量传递到视图中,请检查您的控制器。

UPD1:此外,在应用程序记录上使用计数是没有意义的(顺便说一句,您想将其重命名为 Courses、Classes 或只是 StudentTutor,因为 Application.rb 是所有模型默认继承的模型,同样适用于 ApplicationController )。它总是有一个学生和一个导师(它属于两种模型)。如果要计算相关记录,则要查找学生(如果要计算他的导师)或导师(反之亦然)

【讨论】:

  • 你的意思是application属性`?
  • 我的意思是作为返回相关活动记录数组的模型公共方法。这就是 Rails 约定指定 has_many 和 belongs_to 关系的方式。你不需要明确地对应用程序做任何事情,因为你已经指定了 has_many。
【解决方案2】:

在您的控制器中,您需要设置@application 变量。

def show
  @application = Application.find(params[:id])
end

我强烈建议不要将此类命名为Application。 “应用程序”这个词在 Rails 中有特殊的含义,你几乎肯定会遇到冲突。 ApplicationController 也是如此。

【讨论】:

  • 那么,我应该把上面的代码放在原来的ApplicationController上还是新建一个ApplicationController
  • 您应该从一个新的控制器和模型重新开始,销毁您刚刚创建的名为“Application”的控制器和模型。 Ruby 有很多保留字。通过避免将它们用作名称,您将为自己省去很多麻烦。 reservedwords.herokuapp.com.
猜你喜欢
  • 2016-09-06
  • 2020-01-01
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
相关资源
最近更新 更多