If I have: @name = ''users'' And I want to do @name.singularize.camelize.constantize.find(1) It seems I cannot do so unless the constant User is already present in ObjectSpace. Is there a dynamic way around this? I''m embarrassed to admit this is my best idea so far: name = @name.singularize open( "#{ RAILS_ROOT }/tmp/cache/#{ name }.rb", ''w'' ) do |f| f.puts "class #{ name.camelize } < ActiveRecord::Base\n set_table_name \"#{ @name }\"\nend\n" end require "#{ RAILS_ROOT }/tmp/cache/#{ name }.rb" name = name.camelize.constantize It works fine but then I have to do: Dir.glob("#{RAILS_ROOT}/app/models/*.rb").each { |f| require f } at the end to restore sanity to my Rails app. I realize I can add some more checks to see if the faked model file is already there and all, but please someone tell me there''s a better way. -- Greg Donald http://destiney.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 -~----------~----~----~----~------~----~------~--~---
On Jan 14, 12:44 pm, "Greg Donald" <gdon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If I have: > > @name = ''users'' > > And I want to do > > @name.singularize.camelize.constantize.find(1) > > It seems I cannot do so unless the constant User is already present in > ObjectSpace. Is there a dynamic way around this?require @name ? ///ark --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jan 14, 12:44 pm, "Greg Donald" <gdon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If I have: > > @name = ''users'' > > And I want to do > > @name.singularize.camelize.constantize.find(1)How about: require @name.singularize ///ark --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jan 14, 2008 2:56 PM, Mark Wilden <mark-OCn100epQuBBDgjK7y7TUQ@public.gmane.org> wrote:> require @name ?That works fine if you have the model file already. Please re-read my post.. or not. -- Greg Donald http://destiney.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 -~----------~----~----~----~------~----~------~--~---
On 14 Jan 2008, at 20:44, Greg Donald wrote:> > If I have: > > @name = ''users'' > > And I want to do > > @name.singularize.camelize.constantize.find(1) > > It seems I cannot do so unless the constant User is already present in > ObjectSpace. Is there a dynamic way around this? >Is there no constant just because the user.rb file has not been loaded or is there no user.rb file If the former it should just work, if the latter: klass = Class.new(ActiveRecord::Base) Object.const_set name.camelize, klass klass.reset_table_name klass.find :first Fred> I''m embarrassed to admit this is my best idea so far: > > name = @name.singularize > open( "#{ RAILS_ROOT }/tmp/cache/#{ name }.rb", ''w'' ) do |f| > f.puts "class #{ name.camelize } < ActiveRecord::Base\n > set_table_name \"#{ @name }\"\nend\n" > end > require "#{ RAILS_ROOT }/tmp/cache/#{ name }.rb" > name = name.camelize.constantize > > It works fine but then I have to do: > > Dir.glob("#{RAILS_ROOT}/app/models/*.rb").each { |f| require f } > > at the end to restore sanity to my Rails app. > > I realize I can add some more checks to see if the faked model file is > already there and all, but please someone tell me there''s a better > way. > > > -- > Greg Donald > http://destiney.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://grou--~--~---------~--~----~------------~-------~--~----~ 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 Mon, Jan 14, 2008 at 02:44:47PM -0600, Greg Donald wrote:> If I have: > > @name = ''users'' > > And I want to do > > @name.singularize.camelize.constantize.find(1) > > It seems I cannot do so unless the constant User is already present in > ObjectSpace. Is there a dynamic way around this? > > I''m embarrassed to admit this is my best idea so far: > > name = @name.singularize > open( "#{ RAILS_ROOT }/tmp/cache/#{ name }.rb", ''w'' ) do |f| > f.puts "class #{ name.camelize } < ActiveRecord::Base\n > set_table_name \"#{ @name }\"\nend\n" > end > require "#{ RAILS_ROOT }/tmp/cache/#{ name }.rb" > name = name.camelize.constantize > > It works fine but then I have to do: > > Dir.glob("#{RAILS_ROOT}/app/models/*.rb").each { |f| require f } > > at the end to restore sanity to my Rails app. > > I realize I can add some more checks to see if the faked model file is > already there and all, but please someone tell me there''s a better > way.If I understand correctly, you are looking for a way to wrap an ActiveRecord model around a DB table given the name of the DB table without (necessarily) creating a .rb file for it a priori. If that''s accurate, I think you want this: name = Object.const_set(@name.singularize.camelize, Class.new(ActiveRecord::Base))> Greg Donald--Greg --~--~---------~--~----~------------~-------~--~----~ 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 Jan 14, 1:06 pm, "Greg Donald" <gdon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 14, 2008 2:56 PM, Mark Wilden <m...-OCn100epQuBBDgjK7y7TUQ@public.gmane.org> wrote: > > > require @name ? > > That works fine if you have the model file already. > > Please re-read my post.. or not.I did reread your post, which is why I deleted my message about 30 seconds after I posted it. :) ///ark --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jan 14, 2008 3:07 PM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> klass = Class.new(ActiveRecord::Base) > Object.const_set name.camelize, klass > klass.reset_table_name > > klass.find :firstThat''s it. Thanks. -- Greg Donald http://destiney.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 -~----------~----~----~----~------~----~------~--~---