Hi all, I set up a constant in an initializer file (under /config/initializers/ myfile.rb) like this: MYCONST = Model.find(:all).size The problem is that rake fails to run some operations like db:migrate or db:reset, due to the fact that my Model table doesn''t exist at some stages. Even with a "unless Model.nil?" it fails. How can I test if the model exists to avoid error during rake? I don''t need this constant for rake tasks but only for normal execution of my app. Read you, Jej --~--~---------~--~----~------------~-------~--~----~ 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 Nov 25, 10:05 am, Jej <j...-GANU6spQydw@public.gmane.org> wrote:> Hi all, > > I set up a constant in an initializer file (under /config/initializers/ > myfile.rb) like this: > > MYCONST = Model.find(:all).sizeModel.count is preferred here. Model.find(:all).size loads everything into memory first.> The problem is that rake fails to run some operations like db:migrate > or db:reset, due to the fact that my Model table doesn''t exist at some > stages. Even with a "unless Model.nil?" it fails. > > How can I test if the model exists to avoid error during rake? I don''t > need this constant for rake tasks but only for normal execution of my > app. > > Read you, > JejI think you want defined?(Model), i.e.: MYCONST = Model.count if defined?(Model) Jeff purpleworkshops.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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Model.count is preferred here. Model.find(:all).size loads everything > into memory first.Thanks for this trick.> MYCONST = Model.count if defined?(Model)Yes, perfect! Thanks Jeff. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---