简体   繁体   中英

Checking if an object is an ApplicationRecord in Rails 5

Given this

module Site
  class Translation < ApplicationRecord

Why does this code return false?

2.4.0 :094 > Site::Translation.is_a? ApplicationRecord
 => false 

if

2.4.0 :093 > Site::Translation.superclass
 => ApplicationRecord(abstract) 

what does Site::Translation.is_a? respond to true?

It's because Site::Translation object is class, so it belongs to Class class, which doesn't inherit from ApplicationRecord . If you try the same trick on actual instance of this class, it will return true , as you expect:

Site::Translation.new.is_a?(ApplicationRecord)
# => true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM