mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Better error message for calling columns_hash
When a record does not have a table name, as in the case for a record with `self.abstract_class = true` and no `self.table_name` set the error message raises a cryptic: "ActiveRecord::StatementInvalid: Could not find table ''" this patch now raises a new `TableNotSpecified Error` Fixes: #36274 Co-Authored-By: Eugene Kenny <elkenny@gmail.com>
This commit is contained in:
parent
aae270de9e
commit
fd3532204c
4 changed files with 19 additions and 0 deletions
|
@ -1,3 +1,7 @@
|
|||
* Loading the schema for a model that has no `table_name` raises a `TableNotSpecified` error.
|
||||
|
||||
*Guilherme Mansur*, *Eugene Kenny*
|
||||
|
||||
* PostgreSQL: Fix GROUP BY with ORDER BY virtual count attribute.
|
||||
|
||||
Fixes #36022.
|
||||
|
|
|
@ -38,6 +38,10 @@ module ActiveRecord
|
|||
class AdapterNotSpecified < ActiveRecordError
|
||||
end
|
||||
|
||||
# Raised when a model makes a query but it has not specified an associated table.
|
||||
class TableNotSpecified < ActiveRecordError
|
||||
end
|
||||
|
||||
# Raised when Active Record cannot find database adapter specified in
|
||||
# +config/database.yml+ or programmatically.
|
||||
class AdapterNotFound < ActiveRecordError
|
||||
|
|
|
@ -482,6 +482,9 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def load_schema!
|
||||
unless table_name
|
||||
raise ActiveRecord::TableNotSpecified, "#{self} has no table configured. Set one with #{self}.table_name="
|
||||
end
|
||||
@columns_hash = connection.schema_cache.columns_hash(table_name).except(*ignored_columns)
|
||||
@columns_hash.each do |name, column|
|
||||
define_attribute(
|
||||
|
|
|
@ -1415,6 +1415,14 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
assert_not_includes SymbolIgnoredDeveloper.columns_hash.keys, "first_name"
|
||||
end
|
||||
|
||||
test ".columns_hash raises an error if the record has an empty table name" do
|
||||
expected_message = "FirstAbstractClass has no table configured. Set one with FirstAbstractClass.table_name="
|
||||
exception = assert_raises(ActiveRecord::TableNotSpecified) do
|
||||
FirstAbstractClass.columns_hash
|
||||
end
|
||||
assert_equal expected_message, exception.message
|
||||
end
|
||||
|
||||
test "ignored columns have no attribute methods" do
|
||||
assert_not_respond_to Developer.new, :first_name
|
||||
assert_not_respond_to Developer.new, :first_name=
|
||||
|
|
Loading…
Reference in a new issue