« Return to Thread: Get a list of all foreign keys (with their respective tables

Get a list of all foreign keys (with their respective tables

by comopasta Gr-2 :: Rate this Message:

Reply to Author | View in Thread


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 rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

 « Return to Thread: Get a list of all foreign keys (with their respective tables