Erwin
2009-Apr-08 12:11 UTC
[ActiveRecord] strange issue, would like to understand before I wake up...
running 2.3.2 simple case (db table created : ''emails'' with a :type field) I have the following Email model class Email < ActiveRecord::Base .... end class RequestEmail < Email .... end TESTING in the console : script/console Loading development environment (Rails 2.3.2)>> Email.new=> #<Email id: nil, sender_email: nil, subject: nil, content: nil, user_id: nil, created_at: nil, updated_at: nil, title: "mr", firstname: "", lastname: "Anonymous", type: nil> (Note : type is nil, not "Email" ... ?)>> RequestEmail.new=> #<RequestEmail id: nil, sender_email: nil, subject: nil, content: nil, user_id: nil, created_at: nil, updated_at: nil, title: "mr", firstname: "", lastname: "Anonymous", type: "RequestEmail">>> exitnow TESTING again.. script/console Loading development environment (Rails 2.3.2)>> RequestEmail.newNameError: uninitialized constant RequestEmail ...const_missing I''ll appreciate some feedback , should I buy a 2nd glass ? LOL 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 -~----------~----~----~----~------~----~------~--~---
Erwin
2009-Apr-08 12:38 UTC
Re: strange issue, would like to understand before I wake up...
found a trcik , but I don''t understand why .... In my ApplicationController require_dependency ''email'' On 8 avr, 14:11, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote:> running 2.3.2 > > simple case (db table created : ''emails'' with a :type field) > > I have the following Email model > > class Email < ActiveRecord::Base > .... > end > > class RequestEmail < Email > .... > end > > TESTING in the console : > script/console > Loading development environment (Rails 2.3.2)>> Email.new > > => #<Email id: nil, sender_email: nil, subject: nil, content: nil, > user_id: nil, created_at: nil, updated_at: nil, title: "mr", > firstname: "", lastname: "Anonymous", type: nil> (Note : type is > nil, not "Email" ... ?)>> RequestEmail.new > > => #<RequestEmail id: nil, sender_email: nil, subject: nil, content: > nil, user_id: nil, created_at: nil, updated_at: nil, title: "mr", > firstname: "", lastname: "Anonymous", type: "RequestEmail"> > > >> exit > > now TESTING again.. > script/console > Loading development environment (Rails 2.3.2)>> RequestEmail.new > > NameError: uninitialized constant RequestEmail > ...const_missing > > I''ll appreciate some feedback , should I buy a 2nd glass ? LOL > > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-08 15:08 UTC
Re: [ActiveRecord] strange issue, would like to understand before I wake up...
On 8 Apr 2009, at 13:11, Erwin wrote:> > TESTING in the console : > script/console > Loading development environment (Rails 2.3.2) >>> Email.new > => #<Email id: nil, sender_email: nil, subject: nil, content: nil, > user_id: nil, created_at: nil, updated_at: nil, title: "mr", > firstname: "", lastname: "Anonymous", type: nil> (Note : type is > nil, not "Email" ... ?) >>> RequestEmail.new > => #<RequestEmail id: nil, sender_email: nil, subject: nil, content: > nil, user_id: nil, created_at: nil, updated_at: nil, title: "mr", > firstname: "", lastname: "Anonymous", type: "RequestEmail"> >>> exit > > now TESTING again.. > script/console > Loading development environment (Rails 2.3.2) >>> RequestEmail.new > NameError: uninitialized constant RequestEmail > ...const_missing > > I''ll appreciate some feedback , should I buy a 2nd glass ? LOL >If you try an eval a constant that does not exist const_missing is called (which by default raises NameError) Rails implements a const_missing that tries to load the file containing the constant for you. Rails can''t guess where you''ve put your models, so this only works if you stick to the conventions: email.rb contains Email, FooController is in foo_controller.rb etc. If you use Email before RequestEmail then email.rb is loaded (which defines both those classes) because Rails knows to load email.rb is const_missing triggers on Email. WHen you do things the other way round rails does not know what to do with RequestEmail since there is no request_email.rb. require_dependency works because it loads email.rb Fred> 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 -~----------~----~----~----~------~----~------~--~---