sdsykes / slim_scrooge

SlimScrooge heavily optimises your database interactions
313 stars 25 forks source link

Error with belongs_to that uses :class_name #10

Closed gwinklosky closed 14 years ago

gwinklosky commented 14 years ago

model belongs_to :title, :class_name => "TrackedAttribute", :dependent => :destroy belongs_to :subtitle, :class_name => "TrackedAttribute", :dependent => :destroy

sql generated uses a column of tracked_attribute_id rather than title_id and subtitle_id so there is an SQL error since that column doesn't exist on the table.

I seemed to fix it in callsite.rb using primary_key_name rather than association_foreign_key; but, not sure that won't affect other belongs_to use cases.

def essential_columns(model_class)
  model_class.reflect_on_all_associations.inject([@primary_key]) do |arr, assoc|
    if assoc.options[:dependent] && assoc.macro == :belongs_to
      arr << assoc.primary_key_name
    end
    arr
  end
end
sdsykes commented 14 years ago

This change is correct. Fixed in v1.0.10. Thanks!