今まで、Model名と同名のControllerでないとメソッドが使えないと思い込んでいた。
要するに、item.rb
というModelがあって、item.rb
で設定されたメソッドを使えるのは
items_controller.rb
のみだと勘違いしていた。
でも、そんな事はないという事が分かったので、理解出来ている範囲でまとめてみる。
index_controller.rb
というControllerがあったとする。
class IndexController < ApplicationController
def index
@items = Item.all.order("updated_at DESC").limit(30)
end
end
このようにItem
というモデル名を記述してあげれば、item.rb
に設定されたメソッドやforeign_key
のIDなども
アクセスする事が出来る。