###前提・実現したいこと
以下のような関連付けをしているモデルがあります。
Plan=>Course=>Lesson
deviseのモデルUser
中間テーブル
- course_plan
- bookmark
course_planモデルにpositonカラムがあります。
bookmarkのindexページでpositionカラム順にbookmarkを並べ替えたいのですが、どのようにすればよいでしょうか?
ruby
1class Plan 2 has_many :users 3 4 has_many :course_plans, -> { order(position: :asc) }, dependent: :destroy 5 6 has_many :courses, -> { includes(:course_plans).order('course_plans.position ASC') }, through: :course_plans 7 8 9class CoursePlan 10 belongs_to :course 11 belongs_to :plan 12 13 14class Course < ActiveRecord::Base 15 has_many :lessons, -> { order(chapter: :asc) }, dependent: :destroy 16 accepts_nested_attributes_for :lessons 17 has_many :course_plans 18 has_many :plans, through: :course_plans 19 20 21class Lesson < ActiveRecord::Base 22 belongs_to :course 23 has_many :bookmarks 24 has_many :bookmarked_users, through: :bookmarks, source: :user 25 26 27class Bookmark < ActiveRecord::Base 28 belongs_to :user 29 belongs_to :lesson
###試したこと
現在は苦肉の策で
コースIDが若い順、同一コースの場合はレッスンIDが若い方を上にしています。
ruby
1 def bookmarks 2 @bookmarks = Bookmark.where(user_id: current_user.id).sort_bookmarks 3 @bookmarks = Bookmark.joins(lesson: [:course]).order("course_id").order("chapter").where(user_id: current_user.id) 4 end
joins
やincludes
してみましたが、うまくいきませんでした。
どなたか教えていただけると幸いです。

あなたの回答
tips
プレビュー