I had exactly the same issue posed in this SO question.
My implementation of the answer looks like this:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
##### The next two lines are the important part ######
has_many :notes, class_name: LessonNotes::Engine::Note, foreign_key: "teacher_id"
has_many :notes, class_name: LessonNotes::Engine::Note, foreign_key: "student_id"
has_many :students, class_name: "User",
foreign_key: "teacher_id"
belongs_to :teacher, class_name: "User"
end
However, I'm getting this error: uninitialized constant LessonNotes::Engine::Note
If I delete one of the has_many
associations on the Engine and leave the other, everything works fine though...
UPDATE:
Got rid of the uninitialized constant
error by changing the relevant two lines like this:
has_many :notes, class_name: "::LessonNotes::Engine::Note", foreign_key: "student_id"
has_many :notes, class_name: "::LessonNotes::Engine::Note", foreign_key: "teacher_id"
But now it seems that the second association overrides the first. ActiveRecord only looks for the second foreign key - in this case "teacher_id" - and ignores the first. Is this expected behavior?