diff options
author | Neeraj Singh <neerajdotname@gmail.com> | 2013-05-22 10:37:37 -0400 |
---|---|---|
committer | Neeraj Singh <neerajdotname@gmail.com> | 2013-05-22 10:37:37 -0400 |
commit | 54ce21ee8d59835dba8fa167ad79490fbba0ca17 (patch) | |
tree | db5714199646f70eea66018933d0ecb0d33eb43e /guides | |
parent | cd27ffbdde958d508a51bf237b7dd7e8fe99766c (diff) | |
download | rails-54ce21ee8d59835dba8fa167ad79490fbba0ca17.tar.gz rails-54ce21ee8d59835dba8fa167ad79490fbba0ca17.tar.bz2 rails-54ce21ee8d59835dba8fa167ad79490fbba0ca17.zip |
Added an example for primary_key option
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/association_basics.md | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index 16a5241319..1590f1d81b 100644 --- a/guides/source/association_basics.md +++ b/guides/source/association_basics.md @@ -1511,6 +1511,20 @@ end By convention, Rails assumes that the column used to hold the primary key of the association is `id`. You can override this and explicitly specify the primary key with the `:primary_key` option. +Let's say that `users` table has `id` as the primary_key but it also has +`guid` column. And the requirement is that `todos` table should hold +`guid` column value and not `id` value. This can be achieved like this + +```ruby +class User < ActiveRecord::Base + has_many :todos, 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`. + + ##### `:source` The `:source` option specifies the source association name for a `has_many :through` association. You only need to use this option if the name of the source association cannot be automatically inferred from the association name. |