Question about ActiveRecord auto-magic

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.

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?

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"

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

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

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

In development, it goes every time for max flexibility.

"On Apr 21, 9:18 pm, Hunter Hillegas 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?

thelorax wrote:

"On Apr 21, 9:18 pm, Hunter Hillegas 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.

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

James.

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"

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

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