ruby on rails - Model association that spans across parent app and engine -
i'm making rails engine makes reference current_user
in controller so:
require_dependency "lesson_notes/application_controller" module lessonnotes class notescontroller < applicationcontroller def index if current_user @notes = note.where(student: current_user) if current_user.user_type.name == "student" @notes = note.where(teacher: current_user) if current_user.user_type.name == "teacher" end end end end
this quite verbose , seem instead:
require_dependency "lesson_notes/application_controller" module lessonnotes class notescontroller < applicationcontroller def index @notes = current_user.notes if current_user end end end
however, the user model exists in parent app, not engine.
in note
model have define belongs_to
association:
module lessonnotes class note < activerecord::base belongs_to :student, class_name: "::user" belongs_to :teacher, class_name: "::user" end end
how define other side of association - has_many
- in user
model?
this how ended doing it...
in parent app:
class user < activerecord::base has_many :notes, class_name: lessonnotes::engine::note, foreign_key: "teacher_id" end
Comments
Post a Comment