I cant find out where is an error with this relation:
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
Why I have to add education_id to faculties table?
Each Education has only one faculty and faculty belongs to many educations.
>> 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/ruby-1.9.3-p194@ckdatabase/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in `initialize'
Whichever side you put a ‘belongs_to’ in will invariably have an id of the associated objects – it’s kind of by definition. If you belong_to something you don’t also belong_to another instance.
If you want each Education to have only one Faculty, there are two alternatives:
So you might try this instead: