Ruby on Railsで子テーブルのデータをソートする方法
やりたいこと
Viewで@chapter.tweets的な感じで子テーブルのデータを一覧で表示する際にデータをソートしたい。
方法
親モデルのhas_manyにorder byを指定
$ vi app/models/chapter.rb class Chapter < ActiveRecord::Base has_many :tweets, :order => 'tweet_id DESC' belongs_to :anime attr_accessible :chapter, :end_time, :start_time, :title, :tweet_collect_end_flg, :tweet_collect_start_flg, :anime_id end
参考サイト
- railsでorder byしたいときのメモ