Forum: Ruby on Rails Question about ActiveRecord auto-magic

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
thelorax (Guest)
on 2007-04-22 04:32
(Received via mailing list)
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.
Phlip (Guest)
on 2007-04-22 04:41
(Received via mailing list)
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
Samuel D. (Guest)
on 2007-04-22 04:57
(Received via mailing list)
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"
thelorax (Guest)
on 2007-04-22 05:02
(Received via mailing list)
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
thelorax (Guest)
on 2007-04-22 05:03
(Received via mailing list)
thanks jeff... I was actually writing the above post when u
posted... ;)
Hunter H. (Guest)
on 2007-04-22 05:19
(Received via mailing list)
Note that in production, this query isn't sent each time, just the
first time,

In development, it goes every time for max flexibility.
thelorax (Guest)
on 2007-04-22 05:41
(Received via mailing list)
"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?
Phlip (Guest)
on 2007-04-22 05:53
(Received via mailing list)
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
James S. (Guest)
on 2007-04-22 06:22
(Received via mailing list)
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/
Rick O. (Guest)
on 2007-04-22 06:38
(Received via mailing list)
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
Hunter H. (Guest)
on 2007-04-22 10:15
(Received via mailing list)
It has nothing to do with schema.db. That's a reference file
generated after migrations complete.
thelorax (Guest)
on 2007-04-22 11:15
(Received via mailing list)
Thanks to everyone who replied.  Thank you Rick for clearing this up.
This topic is locked and can not be replied to.