aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/association_basics.md
diff options
context:
space:
mode:
authorGirish S <girish.sonawane@gmail.com>2014-08-26 12:38:05 +0530
committerGirish S <girish.sonawane@gmail.com>2014-09-23 15:21:17 +0530
commit849715ad6728083333ca77debcd6d4744443c9de (patch)
tree7f2b6eb76f4a11fd5c8bef12884b4dbef731496a /guides/source/association_basics.md
parent017294066fd7f54401d13db116f59655553e6b8c (diff)
downloadrails-849715ad6728083333ca77debcd6d4744443c9de.tar.gz
rails-849715ad6728083333ca77debcd6d4744443c9de.tar.bz2
rails-849715ad6728083333ca77debcd6d4744443c9de.zip
added primary_key option documentation for belongs_to association
fixes issue #16698
Diffstat (limited to 'guides/source/association_basics.md')
-rw-r--r--guides/source/association_basics.md19
1 files changed, 19 insertions, 0 deletions
diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md
index c9e0fcd939..026435b208 100644
--- a/guides/source/association_basics.md
+++ b/guides/source/association_basics.md
@@ -827,6 +827,7 @@ The `belongs_to` association supports these options:
* `:counter_cache`
* `:dependent`
* `:foreign_key`
+* `:primary_key`
* `:inverse_of`
* `:polymorphic`
* `:touch`
@@ -908,6 +909,24 @@ end
TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
+##### `:primary_key`
+
+By convention, Rails assumes that the column used to hold the primary key of it's table is `id`, and `:primary_key` allows you to specify a different column.
+
+Let's say that `users` table has `guid` as the primary key. And the requirement is that `todos` table should hold `guid` column value in the foreign key `user_id`. This can be achieved like this
+
+```ruby
+class User < ActiveRecord::Base
+ self.primay_key = 'guid' # primary key is guid and not id
+end
+
+class Todo < ActiveRecord::Base
+ belongs_to :user, primary_key: 'guid'
+end
+```
+
+Now if we execute `@user.todos.create` then `@todo` record will have `user_id` value as the `guid` value of `@user`.
+
##### `:inverse_of`
The `:inverse_of` option specifies the name of the `has_many` or `has_one` association that is the inverse of this association. Does not work in combination with the `:polymorphic` options.