LoginSignup
0
0

More than 5 years have passed since last update.

「Base」クラスをviewから使う

Last updated at Posted at 2018-11-22

Ruby 2.5.3
Rails 5.2.1

ちょっと面白かったのでメモ。

「Base」という名前のmodelがあり、そこにscopeを定義してviewから直接呼び出したい場合、
「Base」classをviewから呼ぶと、下記エラーが発生する。

class Base < ApplicationRecord
  scope :get_awesome_list, -> { joins(:fugas).order(:bar).limit(10) }
<%= Base.get_awesome_list %>
# => undefined method `get_awesome_list' for ActionView::Base:Class

どうやらBaseのことをActionView::Baseクラスと勘違いしているようなので、
superClassを指定してあげるとうまくいく。

<%= ApplicationRecord::Base.get_awesome_list %>
# => awesome_listが返ってくる!

まとめ
予約語をClass名に使うのはやめよう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0