# Question about ActiveRecord auto-magic

**URL:** <https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638>\
**Category:** Rails\
**Created:** [April 22, 2007, 2:32am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638 "2007-04-22T02:32:12Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![thelorax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@thelorax](https://www.ruby-forum.com/u/thelorax)\
**Post date:** [April 22, 2007, 2:32am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638/1 "2007-04-22T02:32:12Z")

</div>

Hello.

When I create a new model instance using Model.new, Rails seems to  
automatically retrieve table meta data (specifically, column names).  
In order to do this, does Rails perform a query in the form of ‘show  
columns from table’ (mysql) or does it somehow discover that  
information without querying the database?

Thanks.

---

<div class="post-metadata">

**Author:** ![thelorax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@thelorax](https://www.ruby-forum.com/u/thelorax)\
**Post date:** [April 22, 2007, 2:41am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638/2 "2007-04-22T02:41:12Z")

</div>

thelorax wrote:

> When I create a new model instance using Model.new, Rails seems to  
> automatically retrieve table meta data (specifically, column names).  
> In order to do this, does Rails perform a query in the form of ‘show  
> columns from table’ (mysql) or does it somehow discover that  
> information without querying the database?

Read your file db/schema.rb, then google for it.

I suspect the system builds schema.rb at boot time, then refers back to  
its  
data structure. Question for the OO nuts - is this the Builder Pattern?

–  
Phlip  
[Test Driven Ajax (on Rails) [Book]](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![thelorax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@thelorax](https://www.ruby-forum.com/u/thelorax)\
**Post date:** [April 22, 2007, 2:57am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638/3 "2007-04-22T02:57:13Z")

</div>

It gets it from the database using a query similar to your example. If  
you look in the console that you’re running mongrel/webrick in, or  
your logs, you can see the specific queries it does to build the  
metadata for each table.

Something like this - “SHOW FIELDS FROM table”

---

<div class="post-metadata">

**Author:** ![thelorax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@thelorax](https://www.ruby-forum.com/u/thelorax)\
**Post date:** [April 22, 2007, 3:03am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638/4 "2007-04-22T03:03:13Z")

</div>

thanks jeff… I was actually writing the above post when u  
posted… 😉

---

<div class="post-metadata">

**Author:** ![thelorax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@thelorax](https://www.ruby-forum.com/u/thelorax)\
**Post date:** [April 22, 2007, 3:19am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638/5 "2007-04-22T03:19:15Z")

</div>

Note that in production, this query isn’t sent each time, just the  
first time,

In development, it goes every time for max flexibility.

---

<div class="post-metadata">

**Author:** ![thelorax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@thelorax](https://www.ruby-forum.com/u/thelorax)\
**Post date:** [April 22, 2007, 3:02am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638/6 "2007-04-22T03:02:13Z")

</div>

Thanks for the reply. After further looking into things it does seem  
that Rails performs a query on the DB when an ActiveRecord instance is  
instantiated.

If you create the model below and perform item=Item.new in the ruby  
console you will see an SQL error in which the following query fails  
‘SHOW FIELDS FROM items’ (mysql). I also manually entered a schema  
definition in db/schema.rb and got the following error. I though  
maybe it would check schema.rb first, and fallback on a query if it  
needed.

class Item \< ActiveRecord::Base  
set\_table\_name ‘items’  
end

---

<div class="post-metadata">

**Author:** ![thelorax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@thelorax](https://www.ruby-forum.com/u/thelorax)\
**Post date:** [April 22, 2007, 3:53am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638/7 "2007-04-22T03:53:16Z")

</div>

thelorax wrote:

> “On Apr 21, 9:18 pm, Hunter H. wrote: Note that in production,  
> this query isn’t sent each time, just the first time.”
> 
> So are you saying that in production the metadata is stored in a flat  
> file and and ActiveRecord will get metadata from this file instead of  
> directly from the DB?

If we are still talking about db/schema.rb, it’s technically a flat  
file,  
yet it is indeed only written once, then read as often as needed. Don’t  
call  
a Ruby file a flat file - that generally means a simple data file.

–  
Phlip  
[Test Driven Ajax (on Rails) [Book]](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![thelorax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@thelorax](https://www.ruby-forum.com/u/thelorax)\
**Post date:** [April 22, 2007, 4:22am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638/8 "2007-04-22T04:22:17Z")

</div>

On 21 Apr 2007, at 21:39, thelorax wrote:

> “On Apr 21, 9:18 pm, Hunter H. wrote: Note that in production,  
> this query isn’t sent each time, just the first time.”
> 
> So are you saying that in production the metadata is stored in a flat  
> file and and ActiveRecord will get metadata from this file instead of  
> directly from the DB?

No, in production mode rails stores the data within the process (in  
memory).

James.

–

> **[Processing Greenbelt 2009](https://jystewart.net/2009/09/08/processing-greenbelt-2009/)**
>
> We did, in fact, make a newspaper. The hour or so after my last post were more than a little stressful, but the results (a few typos and a printing hiccup that most certainly wasn’t our fault…

---

<div class="post-metadata">

**Author:** ![thelorax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@thelorax](https://www.ruby-forum.com/u/thelorax)\
**Post date:** [April 22, 2007, 3:41am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638/9 "2007-04-22T03:41:16Z")

</div>

“On Apr 21, 9:18 pm, Hunter H. wrote: Note that in production,  
this query isn’t sent each time, just the first time.”

So are you saying that in production the metadata is stored in a flat  
file and and ActiveRecord will get metadata from this file instead of  
directly from the DB?

---

<div class="post-metadata">

**Author:** ![thelorax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@thelorax](https://www.ruby-forum.com/u/thelorax)\
**Post date:** [April 22, 2007, 4:38am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638/10 "2007-04-22T04:38:18Z")

</div>

On 4/21/07, James S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> memory).  
> There are a few misconceptions in this thread, so let me try and clear  
> them all up.

- Rails does not perform a query when it instantiates a record,  
technically. It calls it the first time that MyModel.columns is  
accessed. The query fetches the table meta data from the database  
once, and is used for every model instance. So the first time you  
instantiate a record, the query will be called, but that’s it.
- You’ll see these queries repeated a lot in development mode,  
however. Rails unloads your application classes so that the next  
request can load the updated versions.
- schema.rb is not used in a live app. It’s only used to rebuild a  
database schema from scrach using “rake db:schema:load.”
- By default, the schema.rb file will be automatically regenerated  
after each migration. You can do this manually with “rake  
db:schema:load”

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![thelorax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@thelorax](https://www.ruby-forum.com/u/thelorax)\
**Post date:** [April 22, 2007, 8:15am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638/11 "2007-04-22T08:15:30Z")

</div>

It has nothing to do with schema.db. That’s a reference file  
generated after migrations complete.

---

<div class="post-metadata">

**Author:** ![thelorax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@thelorax](https://www.ruby-forum.com/u/thelorax)\
**Post date:** [April 22, 2007, 9:15am UTC](https://www.ruby-forum.com/t/question-about-activerecord-auto-magic/92638/12 "2007-04-22T09:15:33Z")

</div>

Thanks to everyone who replied. Thank you Rick for clearing this up.
