Skip to content
Browse files

Clashing type columns due to a sloppy join shouldn't wreck single-tab…

…le inheritance. Closes #5838.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4813 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent d65a8f6 commit 4b4dd5408228c342f36bfd8290d39de2cf7bbd71 @jeremy jeremy committed Aug 24, 2006
Showing with 29 additions and 11 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +22 −11 activerecord/lib/active_record/base.rb
  3. +5 −0 activerecord/test/finder_test.rb
View
2 activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Clashing type columns due to a sloppy join shouldn't wreck single-table inheritance. #5838 [Kevin Clark]
+
* Fixtures: correct escaping of \n and \r. #5859 [evgeny.zislis@gmail.com]
* Migrations: gracefully handle missing migration files. #5857 [eli.gordon@gmail.com]
View
33 activerecord/lib/active_record/base.rb
@@ -1027,23 +1027,34 @@ def find_some(ids, options)
end
end
- # Finder methods must instantiate through this method to work with the single-table inheritance model
- # that makes it possible to create objects of different types from the same table.
+ # Finder methods must instantiate through this method to work with the
+ # single-table inheritance model that makes it possible to create
+ # objects of different types from the same table.
def instantiate(record)
- object =
+ object =
if subclass_name = record[inheritance_column]
+ # No type given.
if subclass_name.empty?
allocate
+
else
require_association_class(subclass_name)
- begin
- compute_type(subclass_name).allocate
- rescue NameError
- raise SubclassNotFound,
- "The single-table inheritance mechanism failed to locate the subclass: '#{record[inheritance_column]}'. " +
- "This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " +
- "Please rename this column if you didn't intend it to be used for storing the inheritance class " +
- "or overwrite #{self.to_s}.inheritance_column to use another column for that information."
+
+ # Ignore type if no column is present since it was probably
+ # pulled in from a sloppy join.
+ unless self.columns_hash.include?(inheritance_column)
+ allocate
+
+ else
+ begin
+ compute_type(subclass_name).allocate
+ rescue NameError
+ raise SubclassNotFound,
+ "The single-table inheritance mechanism failed to locate the subclass: '#{record[inheritance_column]}'. " +
+ "This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " +
+ "Please rename this column if you didn't intend it to be used for storing the inheritance class " +
+ "or overwrite #{self.to_s}.inheritance_column to use another column for that information."
+ end
end
end
else
View
5 activerecord/test/finder_test.rb
@@ -93,6 +93,11 @@ def test_find_with_prepared_select_statement
assert_equal(topics(:second).title, topics.first.title)
end
+ def test_find_by_sql_with_sti_on_joined_table
+ accounts = Account.find_by_sql("SELECT * FROM accounts INNER JOIN companies ON companies.id = accounts.firm_id")
+ assert_equal [Account], accounts.collect(&:class).uniq
+ end
+
def test_find_first
first = Topic.find(:first, :conditions => "title = 'The First Topic'")
assert_equal(topics(:first).title, first.title)

0 comments on commit 4b4dd54

Please sign in to comment.
Something went wrong with that request. Please try again.