Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Include aliases in attribute names #10518

Closed
wants to merge 1 commit into from

2 participants

Patrick Van Stee Jeremy Kemper
Patrick Van Stee

This came up when trying to include attribute aliases in the list of parameters wrapped by default: #10375. Returning both aliases and names in ActiveRecord#attribute_names seems like the best solution, as recommended by @jeremy.

Jeremy Kemper
Owner

Changing attribute_names directly means that scaffold/admin plugins that read a model's attribute names to show a table of records will now have duplicate columns.

Think this is a good case for introducing new API that returns a list of all attributes that the model responds to.

Patrick Van Stee

Ah yeah I didn't think of that. I guess since I'll be adding a new method and switching it out in the params wrapper I'll just add it to #10375

Patrick Van Stee vanstee closed this
Patrick Van Stee vanstee deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 8, 2013
  1. Patrick Van Stee
This page is out of date. Refresh to see the latest.
4 activerecord/CHANGELOG.md
View
@@ -96,4 +96,8 @@
*Slava Markevich*
+* Include attribute aliases in array returned by `ActiveRecord#attribute_names`.
+
+ *Patrick Van Stee*
+
Please check [4-0-stable](https://github.com/rails/rails/blob/4-0-stable/activerecord/CHANGELOG.md) for previous changes.
2  activerecord/lib/active_record/attribute_methods.rb
View
@@ -109,7 +109,7 @@ def attribute_method?(attribute)
# # => ["id", "created_at", "updated_at", "name", "age"]
def attribute_names
@attribute_names ||= if !abstract_class? && table_exists?
- column_names
+ column_names + attribute_aliases.keys
else
[]
end
4 activerecord/test/cases/base_test.rb
View
@@ -1421,6 +1421,10 @@ def test_attribute_names_on_abstract_class
assert_equal [], AbstractCompany.attribute_names
end
+ def test_attribute_names_with_aliases
+ assert_includes Topic.attribute_names, "heading"
+ end
+
def test_touch_should_raise_error_on_a_new_object
company = Company.new(:rating => 1, :name => "37signals", :firm_name => "37signals")
assert_raises(ActiveRecord::ActiveRecordError) do
Something went wrong with that request. Please try again.