Rails reorder isn't working? -
i have feedback model 2 other models (comment , review) inherit from.
i have default scope in feedback model orders id asc
i've added scope feedback model:
class feedback < activerecord::base include paginatable belongs_to :video default_scope order(:id => :asc) end scope :newest, -> { reorder(:created_at => :desc) } end
i have video model has:
has_many :feedback has_many :latest_feedback, -> { newest }, class_name: 'feedback'
the problem i'm having when call
video.includes(:latest_feedback).find(1)
it still orders feedback id
first , created_at
not want. want latest_feedback
ordered created_at
here's does:
video load (0.3ms) select `videos`.* `videos` `videos`.`id` = 85 order `videos`.`id` desc limit 1 feedback load (0.6ms) select `feedback`.* `feedback` `feedback`.`video_id` in (1) order `feedback`.`id` asc, `feedback`.`created_at` desc
here's need do:
video load (0.3ms) select `videos`.* `videos` `videos`.`id` = 85 order `videos`.`id` desc limit 1 feedback load (0.6ms) select `feedback`.* `feedback` `feedback`.`video_id` in (1) order `feedback`.`created_at` desc
update
i guess known issue rails. there still open pull request fix here:
https://github.com/rails/rails/pull/16531
it doesn't land until rails 5.0.0 though.
you can use video.joins(:latest_feedback).find(1)
. seems rails appends default scope without scope recalculation on includes
Comments
Post a Comment