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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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" On Apr 21, 5:40 pm, "Phlip" <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Apr 21, 8:40 pm, "Phlip" <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
thanks jeff... I was actually writing the above post when u posted... ;) On Apr 21, 9:01 pm, thelorax <adweinst...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On Apr 21, 8:40 pm, "Phlip" <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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, 2007, at 5:56 PM, Jeff wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
"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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Phlip http://www.oreilly.com/catalog/9780596510657/ "Test Driven Ajax (on Rails)" assert_xpath, assert_javascript, & assert_ajax --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 21 Apr 2007, at 21:39, 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?No, in production mode rails stores the data within the process (in memory). James. -- http://jystewart.net/process/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 4/21/07, James Stewart <jystewart-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 21 Apr 2007, at 21:39, 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? > > No, in production mode rails stores the data within the process (in > 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 Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It has nothing to do with schema.db. That''s a reference file generated after migrations complete. On Apr 21, 2007, at 6:51 PM, Phlip wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks to everyone who replied. Thank you Rick for clearing this up. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---