2017-01-28 3 views
1

Я работаю над STMS. В то время как я пытаюсь моделировать вещи и попытаться добавить новую запись для студентов таблицы с помощью:ActiveRecord :: HasManyThroughAssociationNotFoundError - Ruby on Rails

stud = Student.create(:student_id => 1, :first_name => 'Jos', :last_name => 'Norton', :email => '[email protected]', :birthday => '12/05/1995', :subjects => 'English', :username => 'samnorton05', :password => 'Grace02112') 

я получил ошибку:

ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :enrolled_subjects in model Student 

Вот модели, которые я использую:

class Student < ApplicationRecord 

    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 Teacher < ApplicationRecord 

    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 

class Subject < ApplicationRecord 

    has_many :students, through: :enrolled_subjects 
    has_many :teachers, through: :enrolled_subjects 
    has_many :admin_users 

    # scope :search, lambda { |query| where(["name LIKE ?", "%#{query}%"])} 

end 

class EnrolledSubject < ApplicationRecord 
    belongs_to :student 
    belongs_to :subject 
    belongs_to :teacher 
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 

В основном у меня есть 5 таблиц, а именно: учащиеся, учителя, предметы, пользователи admin и зарегистрированные предметы. Любая идея, что я делаю неправильно? Извините, я новичок здесь.

EDIT ВОТ МОЯ МИГРАЦИЯ:

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 

    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 

ответ

1

Вы должны добавить enrolled_subjects, а также, как:

class Student < ApplicationRecord 
    # first you add the association to enrolled_subjects 
    has_many :enrolled_subjects 
    # then you add the associations you has through it 
    has_many :subjects, through: :enrolled_subjects 
    has_many :teachers, through: :enrolled_subjects 

end 
+0

Am Мне также нужно положить, что на учителей, а? –

+0

Я попробовал, и он говорит ** NoMethodError: undefined method 'each 'for" English ": String ** –

+0

yeap! все они, модель должна знать, что она имеет ассоциацию 'has_many' с моделью, чтобы затем создать другую ассоциацию через нее. –

Смежные вопросы