def models_with_attribute(attr)
ActiveRecord::Base.connection.tables.
select{ |table| table.classify.safe_constantize&.has_attribute?(attr) }
end
=> ["ModelA",
"ModelB",
"ModelC"]
-
@QUANONさんから教えてもらった
select
を加えて更にシンプルになりました。
More than 5 years have passed since last update.
def models_with_attribute(attr)
ActiveRecord::Base.connection.tables.
select{ |table| table.classify.safe_constantize&.has_attribute?(attr) }
end
=> ["ModelA",
"ModelB",
"ModelC"]
select
を加えて更にシンプルになりました。 Register as a new user and use Qiita more conveniently