Can anyone explain why the following shows that when you dynamically create an Active::Record object, the accessors/mutators for the columns are not created? Very puzzled! Thanks :o) klass = Class.new(ActiveRecord::Base) do set_table_name table_name end Kernel.const_set(model_class_name, klass) puts "Created the model class and set as a constant." column_names = model_class.columns.map{ |x| x.name } methods = model_class_name.constantize.new.methods raise "Columns not in instance as methods\n\n"+ "Columns:\n\n#{column_names.sort.join("\n")}\n\n"+ "Instance Methods:\n\n#{methods.sort.join("\n")}" if methods & column_names != column_names -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
On 9/19/06, Matthew Painter <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Can anyone explain why the following shows that when you dynamically > create an Active::Record object, the accessors/mutators for the columns > are not created? Very puzzled! Thanks :o) >I''m not sure what''s wrong with the code you listed, but here''s a module I use to create dynamic active record objects. module Stone module Dynamic class << self def klass(table_name) tname = class_name_from_table(table_name) #see if it has already been defined const_missing(tname) rescue NameError define_klass(table_name) end def objeck(table_name) klass(table_name).new end private def class_name_from_table(table_name) Inflector.camelize(table_name) end def define_klass(table_name) tname = class_name_from_table(table_name) class_def = <<-end_eval class #{tname} < ActiveRecord::Base set_table_name(''#{table_name}'') end end_eval eval(class_def, TOPLEVEL_BINDING) const_get(tname) end end end end Hope this is of some use, andy -- Andrew Stone --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jeremy Kemper
2006-Sep-19 19:13 UTC
Re: Can''t dynamically create an Acitve::Record object
On 9/19/06, Matthew Painter <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Can anyone explain why the following shows that when you dynamically > create an Active::Record object, the accessors/mutators for the columns > are not created? Very puzzled! Thanks :o)Try it with a ''normal'' AR object - doesn''t work either. Attribute getters/setters are handled by method_missing, which defines instance methods at runtime for speed. Otherwise, Object.const_set :Person, Class.new(ActiveRecord::Base) is plenty. jeremy --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Matthew Painter
2006-Sep-20 09:26 UTC
Re: Can''t dynamically create an Acitve::Record object
Thanks guys! -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---