Permalink
Browse files

Updated the rake task to take STI into consideration.

  • Loading branch information...
1 parent 5ec90a5 commit cced76cf1026813797042455b5914a57cdbc5c28 @jyurek jyurek committed Jan 13, 2009
Showing with 17 additions and 1 deletion.
  1. +17 −1 tasks/paperclip_tasks.rake
View
@@ -14,10 +14,26 @@ def obtain_attachments
end
end
+def quote_table(table)
+ ActiveRecord::Base.connection.quote_table_name(table)
+end
+
+def quote_column(column)
+ ActiveRecord::Base.connection.quote_column_name(column)
+end
+
+def type_condition(klass)
+ if klass.superclass == ActiveRecord::Base
+ ""
+ else
+ "WHERE #{quote_column(klass.inheritance_column)} = '#{klass.name}'"
+ end
+end
+
def for_all_attachments
klass = obtain_class
names = obtain_attachments
- ids = klass.connection.select_values("SELECT #{klass.primary_key} FROM #{klass.table_name}")
+ ids = klass.connection.select_values("SELECT #{quote_column(klass.primary_key)} FROM #{quote_table(klass.table_name)} #{type_condition(klass)}")
ids.each do |id|
instance = klass.find(id)

0 comments on commit cced76c

Please sign in to comment.