diff options
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb | 8 |
2 files changed, 6 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 838d8d252a..78243608cd 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *CVS* +* Fixed that has_and_belongs_to_many would generate bad sql when naming conventions differed from using vanilla "id" everywhere [RedTerror] + * Added a better exception for when a type column is used in a table without the intention of triggering single-table inheritance. Example: ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'bad_class!'. diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index d53650fbd8..0da243786e 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -3,16 +3,16 @@ module ActiveRecord class HasAndBelongsToManyAssociation < AssociationCollection #:nodoc: def initialize(owner, association_name, association_class_name, association_class_primary_key_name, join_table, options) super(owner, association_name, association_class_name, association_class_primary_key_name, options) - + @association_foreign_key = options[:association_foreign_key] || Inflector.underscore(Inflector.demodulize(association_class_name)) + "_id" association_table_name = options[:table_name] || @association_class.table_name(association_class_name) @join_table = join_table - @order = options[:order] || "t.#{@owner.class.primary_key}" + @order = options[:order] || "t.#{@association_class.primary_key}" interpolate_sql_options!(options, :finder_sql, :delete_sql) @finder_sql = options[:finder_sql] || "SELECT t.*, j.* FROM #{association_table_name} t, #{@join_table} j " + - "WHERE t.#{@owner.class.primary_key} = j.#{@association_foreign_key} AND " + + "WHERE t.#{@association_class.primary_key} = j.#{@association_foreign_key} AND " + "j.#{association_class_primary_key_name} = #{@owner.quoted_id} " + (options[:conditions] ? " AND " + options[:conditions] : "") + " " + "ORDER BY #{@order}" @@ -105,4 +105,4 @@ module ActiveRecord end end end -end
\ No newline at end of file +end |