简体   繁体   中英

ActiveRecord::AssociationTypeMismatch - Ruby on Rails

I've been running with some issues with adding a new record on my students table. Basically I have 3 tables that are associated with each other: Students, Teachers, Enrolled Subjects and Subjects.

The main issue here is that the subjects table was declared :string on both students and teachers table upon migration but is asking for an array instead:

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

Below is my code when adding a record inside 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')  

I also tried:

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')

But I got an error both:

When I used string 'English':

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

When I used array: :subjects => ['English']

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

I am not sure what's happening here. But it's taking time for me to resolve. For my models I simply put the ff codes:

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

Any idea? Sorry I am new ROR. Hope someone can help me and explain in layman's term what's happening on this.

The subjects string column on teachers and students is conflicting with what rails gives you in a has_many association. You'll want to drop these columns.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM