Question about ActiveRecord auto-magic


#1

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.


#2

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
http://www.oreilly.com/catalog/9780596510657/
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax


#3

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”


#4

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


#5

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

In development, it goes every time for max flexibility.


#6

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


#7

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
http://www.oreilly.com/catalog/9780596510657/
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax


#8

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.


http://jystewart.net/process/


#9

“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?


#10

On 4/21/07, James S. 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://weblog.techno-weenie.net
http://mephistoblog.com


#11

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


#12

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