Vijikumar M
Vijikumar M

Reputation: 3764

How ActiveRecord finds the attributes of a model?

here is my question.

I want to know how active record access the tables and columns in a database ?

Upvotes: 1

Views: 1580

Answers (5)

Doon
Doon

Reputation: 20232

It uses introspection. If you watch your debug logs, the first time you access the model you should see a query that is probing for the table definition, columns, etc. I am most familiar with postgres so I'll link to that, but all connection adapters do this is a DB specific way.

take a look at activerecord / lib / active_record / connection_adapters for your database and along with the column_definitions function, which generates the runtime introspection for postgres this looks like this.

(from https://github.com/rails/rails/blob/60fa355790bd6888ab8758bd4989431256d8ba8e/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb)

def column_definitions(table_name) #:nodoc:
          exec_query(<<-end_sql, 'SCHEMA').rows
              SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '#{quote_table_name(table_name)}'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
          end_sql
        end

This information is then cached and used to generate the dynamic methods.

Reading the rails source, while it can very confusing is good fun, and you can see how some of the magic works.

Upvotes: 0

nickcen
nickcen

Reputation: 1692

Take mysql as an example, it has a db call information_schema, and there are table name tables, columns and so on. Mysql use these tables to store the info of each table and its column definition inside other mysql's databases.

Other db provicer may have something similar to that.

Upvotes: 0

Simone Carletti
Simone Carletti

Reputation: 176402

The majority of the code that answers you question is contained in the model_schema.rb file.

The table_name is the name of the database table corresponding to your model. As explained in the documentation, it is guessed from the model name unless explicitly set.

Guesses the table name (in forced lower-case) based on the name of the class in the inheritance hierarchy descending directly from ActiveRecord::Base. So if the hierarchy looks like: Reply < Message < ActiveRecord::Base, then Message is used to guess the table name even when called on Reply. The rules used to do the guess are handled by the Inflector class in Active Support, which knows almost all common English inflections. You can add new inflections in config/initializers/inflections.rb.

Nested classes are given table names prefixed by the singular form of the parent's table name. Enclosing modules are not considered. You can also set your own table name explicitly:

class Mouse < ActiveRecord::Base
  self.table_name = "mice"
end

Alternatively, you can override the table_name method to define your own computation. (Possibly using super to manipulate the default table name.) Example:

class Post < ActiveRecord::Base
  def self.table_name
    "special_" + super
  end
end

For the columns, it's a little bit more complicated. You can start from the columns method that fetches the column definition from the schema.rb file and returns them.

Columns are explicitly defined in the schema.rb file inside your application.

Upvotes: 3

memRistor
memRistor

Reputation: 101

ActiveRecord provides a Base class, which, when subclassed, sets up a mapping between the said class and an existing DB table.

Each finder method allows you to pass arguments into it to perform certain queries on your database without writing raw SQL.

If you're looking for more on the inner workings, here's a good place to start: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb

Upvotes: 0

Nitin Jain
Nitin Jain

Reputation: 3083

It is done by ORM layer and it follows the rails convention to map the table and there field until you manually configure your model for mapping a table in database

take a look at http://guides.rubyonrails.org/active_record_basics.html

Upvotes: 0

Related Questions