aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-03-03 00:18:57 +0100
committerXavier Noria <fxn@hashref.com>2010-03-03 00:18:57 +0100
commita368f3b170a30ac9c5f479ba6a62bc4d1b82c972 (patch)
tree9da7a9cee403dcd017e8aca4ce6180f710c72035 /railties/guides/source
parent3100ec4355320c088e0969bd5caf62c5c0042f4a (diff)
downloadrails-a368f3b170a30ac9c5f479ba6a62bc4d1b82c972.tar.gz
rails-a368f3b170a30ac9c5f479ba6a62bc4d1b82c972.tar.bz2
rails-a368f3b170a30ac9c5f479ba6a62bc4d1b82c972.zip
AS guide: documents String#foreign_key
Diffstat (limited to 'railties/guides/source')
-rw-r--r--railties/guides/source/active_support_core_extensions.textile25
1 files changed, 25 insertions, 0 deletions
diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile
index 3370939861..368dcc00b6 100644
--- a/railties/guides/source/active_support_core_extensions.textile
+++ b/railties/guides/source/active_support_core_extensions.textile
@@ -1556,6 +1556,31 @@ end
NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+h5. +foreign_key+
+
+The method +foreign_key+ gives a foreign key column name from a class name. To do so it demodulizes, underscores, and adds "_id":
+
+<ruby>
+"User".foreign_key # => "user_id"
+"InvoiceLine".foreign_key # => "invoice_line_id"
+"Admin::Session".foreign_key # => "session_id"
+</ruby>
+
+Pass a false argument if you do not want the underscore in "_id":
+
+<ruby>
+"User".foreign_key(false) # => "userid"
+</ruby>
+
+Associations use this method to infer foreign keys, for example +has_one+ and +has_many+ do this:
+
+<ruby>
+# active_record/associations.rb
+foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
+</ruby>
+
+NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+
h3. Extensions to +Numeric+
h4. Bytes