I have a Rails model:
class Foo < ActiveRecord::Base
...
end
Given an active record relation:
limited_foo = Foo.where(...)
how can I get the original model class?
limited_foo ... # => Foo
The Relation Class. Having queries return an ActiveRecord::Relation object allows us to chain queries together and this Relation class is at the heart of the new query syntax. Let's take a look at this class by searching through the ActiveRecord source code for a file called relation.
Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.
Creates an object (or multiple objects) and saves it to the database, if validations pass. The resulting object is returned whether the object was saved successfully to the database or not.
Active Model is a library containing various modules used in developing classes that need some features present on Active Record.
ActiveRecord::Relation
has an attribute_reader klass
:
limited_foo.klass
#=> Foo
Source is here. Alias model
works as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With