diff options
author | Xavier Noria <fxn@hashref.com> | 2009-09-13 21:24:04 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2009-09-13 21:24:04 +0200 |
commit | a0fb9a6f7c0614358e59bb5f95eef00cba79ab50 (patch) | |
tree | 2356573301aa9f3038108e8f86793704050bb1f0 /railties/guides | |
parent | 115aa4eee07053947f3fa76d1a118ebb8d67e5ed (diff) | |
download | rails-a0fb9a6f7c0614358e59bb5f95eef00cba79ab50.tar.gz rails-a0fb9a6f7c0614358e59bb5f95eef00cba79ab50.tar.bz2 rails-a0fb9a6f7c0614358e59bb5f95eef00cba79ab50.zip |
AS guide: documents assert_valid_keys
Diffstat (limited to 'railties/guides')
-rw-r--r-- | railties/guides/source/active_support_overview.textile | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index 2e7fbe7cea..a498849633 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/railties/guides/source/active_support_overview.textile @@ -1258,6 +1258,37 @@ h5. +to_options+ and +to_options!+ The methods +to_options+ and +to_options!+ are respectively aliases of +symbolize_keys+ and +symbolize_keys!+. +h5. +assert_valid_keys+ + +The method +assert_valid_keys+ receives an arbitrary number of arguments, and checks whether the receiver has any key outside that white list. If it does +ArgumentError+ is raised. + +<ruby> +{:a => 1}.assert_valid_keys(:a) # passes +{:a => 1}.assert_valid_keys("a") # ArgumentError +</ruby> + +Active Record does not accept unknown options when building associations for example. It implements that control via +assert_valid_keys+: + +<ruby> +mattr_accessor :valid_keys_for_has_many_association +@@valid_keys_for_has_many_association = [ + :class_name, :table_name, :foreign_key, :primary_key, + :dependent, + :select, :conditions, :include, :order, :group, :having, :limit, :offset, + :as, :through, :source, :source_type, + :uniq, + :finder_sql, :counter_sql, + :before_add, :after_add, :before_remove, :after_remove, + :extend, :readonly, + :validate, :inverse_of +] + +def create_has_many_reflection(association_id, options, &extension) + options.assert_valid_keys(valid_keys_for_has_many_association) + ... +end +</ruby> + h4. Indifferent Access The method +with_indifferent_access+ returns an +ActiveSupport::HashWithIndifferentAccess+ out of its receiver: |