Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Get outer class name inside concern

Tags:

How can I get the class name of a model I am including a concern into?

I am creating a concern for a scope as described in here https://stackoverflow.com/a/14483808/1715035 but need the model name for polymorphic search.

self.table_name works fine and so does self.table_name.singularize.titleize.gsub(/\s+/, "") but maybe theres an easier way to get the class name.

like image 711
WhoDidThis Avatar asked Jan 25 '13 00:01

WhoDidThis


1 Answers

You can use the name method as you would in the class itself.

module M
  extend ActiveSupport::Concern

  included do
    scope :my_fancy_scope, where(some_type: name)
  end
end
like image 144
Andrew Haines Avatar answered Oct 11 '22 07:10

Andrew Haines