0

I have a many-to-many relationship between a model User and Picture. These are linked by a join table called Picturization.

If I obtain a list of users of a single picture, i.e. picture.users -> how can I ensure that the result obtained is sorted by either creation of the Picturization row (i.e. the order at which a picture was associated to a user). How would this change if I wanted to obtain this in order of modification?

Thanks!

Edit Maybe something like

picture.users.where(:order => "created_at")
  • but this created_at refers to the created_at in picturization
4

3 に答える 3

1

画像化テーブルにシーケンスのような追加の列を用意し、画像化のデフォルトスコープとして並べ替え順序を定義します

default_scope :order => 'sequence ASC'

modify_atに基づくデフォルトの並べ替え順序が必要な場合は、次のデフォルトスコープを使用します

default_scope :order => 'modified_at DESC'
于 2012-05-02T16:48:10.700 に答える
0

order メソッド/句でテーブル名を指定できます。

picture.users.order("picturizations.created_at DESC")
于 2012-05-02T19:39:07.480 に答える