0
我無法找出與此關係的錯誤:HAS_ONE協會工作不
class Education < ActiveRecord::Base
attr_accessible :description, :graduation, :student_id, :faculty_id, :department_id
has_one :faculty
belongs_to :student
end
# id :integer not null, primary key
# student_id :integer
# faculty_id :integer
# description :text
# graduation :string(255)
class Faculty < ActiveRecord::Base
attr_accessible :department_id, :name
belongs_to :education
belongs_to :department
end
# == Schema Information
#
# Table name: faculties
#
# id :integer not null, primary key
# name :string(255)
# department_id :integer
爲什麼我要補充education_id
到faculties
表? 每個教育只有一個教師和教師屬於許多教育。
>> Education.last.faculty
Education Load (0.3ms) SELECT "educations".* FROM "educations" ORDER BY "educations"."id" DESC LIMIT 1
Faculty Load (0.2ms) SELECT "faculties".* FROM "faculties" WHERE "faculties"."education_id" = 1 LIMIT 1
SQLite3::SQLException: no such column: faculties.education_id: SELECT "faculties".* FROM "faculties" WHERE "faculties"."education_id" = 1 LIMIT 1
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: faculties.education_id: SELECT "faculties".* FROM "faculties" WHERE "faculties"."education_id" = 1 LIMIT 1
from /Users/rege/.rvm/gems/[email protected]/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in `initialize'
另一個很好的資源是http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to –