On 17 Sep 2008, at 19:00, Pardee, Roy wrote:

>
> Re: generating a list of model classes, I was hoping something like  
> this would work:
>
>  ObjectSpace.each_object(ActiveRecord::Base) {|o| puts(o)}
>
> But no dice (in a rails console anyway)--that will enumerate  
> instances of model classes, but not the classes themselves.  I guess  
> ObjectSpace doesn't consider classes objects?  Confusing...
>
Classes are objects, but subclasses of ActiveRecord::Base are instance  
of ActiveRecord::Base, they're instances of Class. You sort of have to  
walk app/models anyway, to make sure all the classes are actually  
loaded.

Fred
> I guess you could troll the files under /app/models...  But you were  
> looking to go *less* hacky, eh? ;-)
>
> -----Original Message-----
> From: [email protected] 
> [mailto:[email protected] 
> ] On Behalf Of Max Williams
> Sent: Wednesday, September 17, 2008 9:35 AM
> To: [email protected]
> Subject: [Rails] Get a list of all foreign keys (with their  
> respective tables
>
>
> As a general speed-up, i want to index all of my foreign keys.   
> Rather than hand-code them all into a migration (thus probably  
> making some mistakes), i'd like to make a little script, or method  
> to do it.
>
> I thought of maybe something like
>
> sql = ActiveRecord::Base.connection
> klasses = <get all model classes somehow> klasses.each do |klass|
>  foreign_key_columns = klass.column_names.select{|s| s.match(/_id$/)}
>  foreign_key_columns.each do |column_name|
>    sql.execute("alter table #{klass.tablename} add index
> (#{column_name})")
>  end
> end
>
> but, this seems a bit hacky and dirty.  Can anyone see a nicer way?
> And, how can i get all of my model classes?
>
> I'm using mysql, in case that's relevant.
>
> thanks
> max
> --
> 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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to