今日勉強したメモ。
テーブルデータの検索で頻繁に使う処理はscope
を使って、定義しておくのがいいみたい。
(例)記事一覧が最新記事順で並べられるようにEntryクラスにscopeを追加する
【モデル】
定義する
./app/models/entry.rb
:latest
とシンボルを設定。
class Entry < ActiveRecord::Base
scope :latest , order ('update_at desc')
end
【コントローラー】
./app/controller/entries_controller.rb
class EntriesController < ApplicationController
def index
@entries = Entry.where("blogger_id=?" , @blogger.id).latest
end
end