ActiveRecord :: AssociationTypeMismatch - Ruby on Rails

时间:2017-01-28 15:21:27

标签: ruby-on-rails ruby activerecord

我在学生桌上添加新记录时遇到了一些问题。基本上我有3个相互关联的表:学生,教师,注册科目和科目。

这里的主要问题是主题表被声明:在迁移时学生和教师表上的字符串,但是要求的是数组:

Class CreateStudents < ActiveRecord::Migration[5.0]

  def up
  create_table :students, :id => false do |t|
    t.integer "student_id", :auto_increment => true, :primary_key => true
    t.string "first_name", :limit => 25
    t.string "last_name", :limit => 50
    t.string "email", :default => ' ', :null => false
    t.string "birthday"
    t.string "subjects"
    t.string "username", :limit => 25
    t.string "password_digest"
    t.timestamps
  end
  end

    class CreateTeachers < ActiveRecord::Migration[5.0]
      def up
      create_table :teachers, :id => false do |t|
          t.integer "teacher_id", :auto_increment => true, :primary_key => true
          t.string "first_name"
          t.string "last_name"
          t.string "email", :default => ' ', :null => false
          t.string "birthday"
          t.string "subjects"
          t.string "username", :limit => 25
          t.string "password_digest"
          t.timestamps
        end
      end

class CreateSubjects < ActiveRecord::Migration[5.0]
  def up
    create_table :subjects, :id => false do |t|
       t.integer "subject_id", :auto_increment => true, :primary_key => true
       t.string "subject_name"
       t.timestamps
    end
  end

class CreateEnrolledSubjects < ActiveRecord::Migration[5.0]
  def up
    create_table :enrolled_subjects, :id => false do |t|
      t.integer "subject_id"
      t.integer "teacher_id"
      t.integer "student_id"
    end
  end

在rails console中添加记录时,下面是我的代码:

new_studend = Student.create(:student_id => 1, :first_name => 'Karl', :last_name => 'Geek', :email => 'ss.norton@gmail.com', :birthday => '12/04/1995', :subjects => ["English"], :username => 'samnorton2', :password => 'Grace02112')  

我也尝试过:

new_student = Student.create(:student_id => 1, :first_name => 'Jos', :last_name => 'Norton', :email => 'ss.norton@gmail.com', :birthday => '12/05/1995', :subjects => 'English', :username => 'samnorton2', :password => 'Grace02112')

但是我得到了一个错误:

当我使用字符串&#39;英语&#39;:

NoMethodError: undefined method `each' for "English":String

当我使用array :: subject =&gt; [&#39;英语&#39;]

ActiveRecord::AssociationTypeMismatch: Subject(#70237743894000) expected, got String(#70237706174560)

我不确定这里发生了什么。但是我需要时间来解决。对于我的模型,我只需输入ff代码:

class Student < ApplicationRecord

  has_many :enrolled_subjects
  has_many :subjects, through: :enrolled_subjects
  has_many :teachers, through: :enrolled_subjects

  def teacher_names
    self.teachers.map(&:name).join(", ")
  end

  has_many :admin_users
  has_secure_password
  self.primary_key = :student_id


  scope :newest_first, lambda { order("created_at ASC") }
  scope :oldest_first, lambda { order("created_at DESC") }
  # scope :search, lambda { |query| where(["name LIKE ?", "%#{query}%"])}

end

    class AdminUser < ApplicationRecord

      has_secure_password

      scope :newest_first, lambda { order("created_at ASC") }
      scope :oldest_first, lambda { order("created_at DESC") }
      # scope :search, lambda { |query| where(["name LIKE ?", "%#{query}%"])}
    end

    class EnrolledSubject < ApplicationRecord
      belongs_to :student
      belongs_to :subject
      belongs_to :teacher
    end

class Teacher < ApplicationRecord

  has_many :enrolled_subjects

  has_many :subjects, through: :enrolled_subjects
  has_many :students, through: :enrolled_subjects
  has_many :admin_users
  has_secure_password

  scope :newest_first, lambda { order("created_at ASC") }
  scope :oldest_first, lambda { order("created_at DESC") }
  # scope :search, lambda { |query| where(["name LIKE ?", "%#{query}%"])}
end

有什么想法吗?对不起,我是新的ROR。希望有人可以帮助我,并以非专业人士的名义解释这件事发生了什么。

1 个答案:

答案 0 :(得分:0)

教师和学生的subjects字符串列与rails在has_many关联中提供的内容相冲突。您将要删除这些列。

def change
  remove_column :students, :subjects
  remove_column :teachers, :subjects
end