Suppose I have a model:
class Post end posts = Post.where(***) puts posts.class # => ActiveRecord::Relation
Then how can I get the model class name through the variable 'posts', maybe some method called model_class_name: puts posts.model_class_name # => Message
Thank:)
The attribute #klass ActiveRecord :: Relation returns the class of the model on which the relation was built:
arel = User.where(name: "fred") arel.klass # User
To get the class name:
arel.klass.name
Tested in ActiveRecord 4.2.4
:
posts.first.class.name
posts.[0].class.name
, . (ActiveRecord:: Relation Ruby Enumerable).
-
Source: https://habr.com/ru/post/1776624/More articles:Hiding a specific image channel using C # - c #Php.ini configuration changes in shared hosting account - phpКак решить, какой из них следует использовать в веб-приложении MVC или трехуровневой архитектуре? - architectureBASH shell script to quickly create and populate new test files - scriptingDoes GraphicsMagick have the equivalent ImageMagick option 'convert -auto-orient'? - image-processingHow to determine if email is a Google Account? - apiJSF cyclic control detection error - javaAccess Gmail through C # .NET - c #Secure Razor Template Engine - asp.net-mvcDo you have a wrapper for your IoC? - dependency-injectionAll Articles