globe-composite_primary_keys 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. data/History.txt +203 -0
  2. data/Manifest.txt +121 -0
  3. data/README.txt +41 -0
  4. data/README_DB2.txt +33 -0
  5. data/Rakefile +30 -0
  6. data/composite_primary_keys.gemspec +17 -0
  7. data/lib/adapter_helper/base.rb +63 -0
  8. data/lib/adapter_helper/mysql.rb +13 -0
  9. data/lib/adapter_helper/oracle.rb +12 -0
  10. data/lib/adapter_helper/oracle_enhanced.rb +12 -0
  11. data/lib/adapter_helper/postgresql.rb +13 -0
  12. data/lib/adapter_helper/sqlite3.rb +13 -0
  13. data/lib/composite_primary_keys.rb +63 -0
  14. data/lib/composite_primary_keys/association_preload.rb +162 -0
  15. data/lib/composite_primary_keys/associations.rb +159 -0
  16. data/lib/composite_primary_keys/attribute_methods.rb +84 -0
  17. data/lib/composite_primary_keys/base.rb +200 -0
  18. data/lib/composite_primary_keys/composite_arrays.rb +29 -0
  19. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +9 -0
  20. data/lib/composite_primary_keys/connection_adapters/ibm_db_adapter.rb +21 -0
  21. data/lib/composite_primary_keys/connection_adapters/oracle_adapter.rb +15 -0
  22. data/lib/composite_primary_keys/connection_adapters/oracle_enhanced_adapter.rb +17 -0
  23. data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +53 -0
  24. data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +15 -0
  25. data/lib/composite_primary_keys/finder_methods.rb +68 -0
  26. data/lib/composite_primary_keys/fixtures.rb +8 -0
  27. data/lib/composite_primary_keys/read.rb +25 -0
  28. data/lib/composite_primary_keys/reflection.rb +39 -0
  29. data/lib/composite_primary_keys/relation.rb +31 -0
  30. data/lib/composite_primary_keys/through_association_scope.rb +212 -0
  31. data/lib/composite_primary_keys/validations/uniqueness.rb +118 -0
  32. data/lib/composite_primary_keys/version.rb +9 -0
  33. data/loader.rb +24 -0
  34. data/local/database_connections.rb.sample +12 -0
  35. data/local/paths.rb.sample +2 -0
  36. data/local/tasks.rb.sample +2 -0
  37. data/scripts/console.rb +48 -0
  38. data/scripts/txt2html +67 -0
  39. data/scripts/txt2js +59 -0
  40. data/tasks/activerecord_selection.rake +43 -0
  41. data/tasks/databases.rake +12 -0
  42. data/tasks/databases/mysql.rake +30 -0
  43. data/tasks/databases/oracle.rake +25 -0
  44. data/tasks/databases/postgresql.rake +25 -0
  45. data/tasks/databases/sqlite3.rake +28 -0
  46. data/tasks/deployment.rake +22 -0
  47. data/tasks/local_setup.rake +13 -0
  48. data/tasks/website.rake +18 -0
  49. data/test/README_tests.txt +67 -0
  50. data/test/abstract_unit.rb +103 -0
  51. data/test/connections/native_ibm_db/connection.rb +23 -0
  52. data/test/connections/native_mysql/connection.rb +13 -0
  53. data/test/connections/native_oracle/connection.rb +14 -0
  54. data/test/connections/native_oracle_enhanced/connection.rb +20 -0
  55. data/test/connections/native_postgresql/connection.rb +8 -0
  56. data/test/connections/native_sqlite/connection.rb +9 -0
  57. data/test/fixtures/article.rb +5 -0
  58. data/test/fixtures/article_group.rb +4 -0
  59. data/test/fixtures/article_groups.yml +7 -0
  60. data/test/fixtures/articles.yml +6 -0
  61. data/test/fixtures/comment.rb +6 -0
  62. data/test/fixtures/comments.yml +16 -0
  63. data/test/fixtures/db_definitions/db2-create-tables.sql +113 -0
  64. data/test/fixtures/db_definitions/db2-drop-tables.sql +16 -0
  65. data/test/fixtures/db_definitions/mysql.sql +181 -0
  66. data/test/fixtures/db_definitions/oracle.drop.sql +39 -0
  67. data/test/fixtures/db_definitions/oracle.sql +188 -0
  68. data/test/fixtures/db_definitions/postgresql.sql +206 -0
  69. data/test/fixtures/db_definitions/sqlite.sql +166 -0
  70. data/test/fixtures/department.rb +5 -0
  71. data/test/fixtures/departments.yml +3 -0
  72. data/test/fixtures/dorm.rb +3 -0
  73. data/test/fixtures/dorms.yml +2 -0
  74. data/test/fixtures/employee.rb +4 -0
  75. data/test/fixtures/employees.yml +9 -0
  76. data/test/fixtures/group.rb +3 -0
  77. data/test/fixtures/groups.yml +3 -0
  78. data/test/fixtures/hack.rb +6 -0
  79. data/test/fixtures/hacks.yml +2 -0
  80. data/test/fixtures/kitchen_sink.rb +3 -0
  81. data/test/fixtures/kitchen_sinks.yml +5 -0
  82. data/test/fixtures/membership.rb +10 -0
  83. data/test/fixtures/membership_status.rb +3 -0
  84. data/test/fixtures/membership_statuses.yml +10 -0
  85. data/test/fixtures/memberships.yml +6 -0
  86. data/test/fixtures/product.rb +7 -0
  87. data/test/fixtures/product_tariff.rb +5 -0
  88. data/test/fixtures/product_tariffs.yml +12 -0
  89. data/test/fixtures/products.yml +6 -0
  90. data/test/fixtures/reading.rb +4 -0
  91. data/test/fixtures/readings.yml +10 -0
  92. data/test/fixtures/reference_code.rb +7 -0
  93. data/test/fixtures/reference_codes.yml +28 -0
  94. data/test/fixtures/reference_type.rb +7 -0
  95. data/test/fixtures/reference_types.yml +9 -0
  96. data/test/fixtures/restaurant.rb +6 -0
  97. data/test/fixtures/restaurants.yml +5 -0
  98. data/test/fixtures/restaurants_suburbs.yml +11 -0
  99. data/test/fixtures/room.rb +10 -0
  100. data/test/fixtures/room_assignment.rb +4 -0
  101. data/test/fixtures/room_assignments.yml +4 -0
  102. data/test/fixtures/room_attribute.rb +3 -0
  103. data/test/fixtures/room_attribute_assignment.rb +5 -0
  104. data/test/fixtures/room_attribute_assignments.yml +4 -0
  105. data/test/fixtures/room_attributes.yml +3 -0
  106. data/test/fixtures/rooms.yml +3 -0
  107. data/test/fixtures/seat.rb +5 -0
  108. data/test/fixtures/seats.yml +4 -0
  109. data/test/fixtures/street.rb +3 -0
  110. data/test/fixtures/streets.yml +15 -0
  111. data/test/fixtures/student.rb +4 -0
  112. data/test/fixtures/students.yml +2 -0
  113. data/test/fixtures/suburb.rb +6 -0
  114. data/test/fixtures/suburbs.yml +9 -0
  115. data/test/fixtures/tariff.rb +6 -0
  116. data/test/fixtures/tariffs.yml +13 -0
  117. data/test/fixtures/user.rb +10 -0
  118. data/test/fixtures/users.yml +6 -0
  119. data/test/hash_tricks.rb +34 -0
  120. data/test/plugins/pagination.rb +405 -0
  121. data/test/plugins/pagination_helper.rb +135 -0
  122. data/test/test_associations.rb +178 -0
  123. data/test/test_attribute_methods.rb +22 -0
  124. data/test/test_attributes.rb +80 -0
  125. data/test/test_clone.rb +34 -0
  126. data/test/test_composite_arrays.rb +32 -0
  127. data/test/test_create.rb +68 -0
  128. data/test/test_delete.rb +83 -0
  129. data/test/test_exists.rb +25 -0
  130. data/test/test_find.rb +73 -0
  131. data/test/test_ids.rb +90 -0
  132. data/test/test_miscellaneous.rb +39 -0
  133. data/test/test_pagination.rb +38 -0
  134. data/test/test_polymorphic.rb +32 -0
  135. data/test/test_santiago.rb +27 -0
  136. data/test/test_suite.rb +19 -0
  137. data/test/test_tutorial_example.rb +26 -0
  138. data/test/test_update.rb +40 -0
  139. data/test/test_validations.rb +11 -0
  140. data/website/index.html +195 -0
  141. data/website/index.txt +159 -0
  142. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  143. data/website/stylesheets/screen.css +126 -0
  144. data/website/template.js +3 -0
  145. data/website/template.rhtml +53 -0
  146. data/website/version-raw.js +3 -0
  147. data/website/version-raw.txt +2 -0
  148. data/website/version.js +4 -0
  149. data/website/version.txt +3 -0
  150. metadata +339 -0
@@ -0,0 +1,32 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/comment'
3
+ require 'fixtures/article'
4
+ require 'fixtures/reading'
5
+ require 'fixtures/user'
6
+ require 'fixtures/employee'
7
+ require 'fixtures/hack'
8
+
9
+ class TestPolymorphic < ActiveSupport::TestCase
10
+ fixtures :users, :employees, :comments, :hacks
11
+
12
+ def test_polymorphic_has_many
13
+ comments = Hack.find('andrew').comments
14
+ assert_equal 'andrew', comments[0].person_id
15
+ end
16
+
17
+ def test_polymorphic_has_one
18
+ first_comment = Hack.find('andrew').first_comment
19
+ assert_equal 'andrew', first_comment.person_id
20
+ end
21
+
22
+ def test_has_many_through
23
+ user = users(:santiago)
24
+ article_names = user.articles.collect { |a| a.name }.sort
25
+ assert_equal ['Article One', 'Article Two'], article_names
26
+ end
27
+
28
+ def test_polymorphic_has_many_through
29
+ user = users(:santiago)
30
+ assert_equal ['andrew'], user.hacks.collect { |a| a.name }.sort
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ # Test cases devised by Santiago that broke the Composite Primary Keys
2
+ # code at one point in time. But no more!!!
3
+
4
+ require 'abstract_unit'
5
+ require 'fixtures/user'
6
+ require 'fixtures/article'
7
+ require 'fixtures/reading'
8
+
9
+ class TestSantiago < ActiveSupport::TestCase
10
+ fixtures :suburbs, :streets, :users, :articles, :readings
11
+
12
+ def test_normal_and_composite_associations
13
+ assert_not_nil @suburb = Suburb.find(1,1)
14
+ assert_equal 1, @suburb.streets.length
15
+
16
+ assert_not_nil @street = Street.find(1)
17
+ assert_not_nil @street.suburb
18
+ end
19
+
20
+ def test_single_keys
21
+ @santiago = User.find(1)
22
+ assert_not_nil @santiago.articles
23
+ assert_equal 2, @santiago.articles.length
24
+ assert_not_nil @santiago.readings
25
+ assert_equal 2, @santiago.readings.length
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+
3
+ require 'test_associations'
4
+ require 'test_attribute_methods'
5
+ require 'test_attributes'
6
+ require 'test_clone'
7
+ require 'test_composite_arrays'
8
+ require 'test_create'
9
+ require 'test_delete'
10
+ require 'test_exists'
11
+ require 'test_find'
12
+ require 'test_ids'
13
+ require 'test_miscellaneous'
14
+ require 'test_pagination'
15
+ require 'test_polymorphic'
16
+ require 'test_santiago'
17
+ require 'test_tutorial_example'
18
+ require 'test_update'
19
+ require 'test_validations'
@@ -0,0 +1,26 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/user'
3
+ require 'fixtures/group'
4
+ require 'fixtures/membership_status'
5
+ require 'fixtures/membership'
6
+
7
+ class TestTutorialExample < ActiveSupport::TestCase
8
+ fixtures :users, :groups, :memberships, :membership_statuses
9
+
10
+ def test_membership
11
+ assert(membership = Membership.find(1,1), "Cannot find a membership")
12
+ assert(membership.user)
13
+ assert(membership.group)
14
+ end
15
+
16
+ def test_status
17
+ assert(membership = Membership.find(1,1), "Cannot find a membership")
18
+ assert(statuses = membership.statuses, "No has_many association to status")
19
+ assert_equal(membership, statuses.first.membership)
20
+ end
21
+
22
+ def test_count
23
+ assert(membership = Membership.find(1,1), "Cannot find a membership")
24
+ assert_equal(1, membership.statuses.count)
25
+ end
26
+ end
@@ -0,0 +1,40 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/reference_type'
3
+ require 'fixtures/reference_code'
4
+
5
+ class TestUpdate < ActiveSupport::TestCase
6
+ fixtures :reference_types, :reference_codes
7
+
8
+ CLASSES = {
9
+ :single => {
10
+ :class => ReferenceType,
11
+ :primary_keys => :reference_type_id,
12
+ :update => { :description => 'RT Desc' },
13
+ },
14
+ :dual => {
15
+ :class => ReferenceCode,
16
+ :primary_keys => [:reference_type_id, :reference_code],
17
+ :update => { :description => 'RT Desc' },
18
+ },
19
+ }
20
+
21
+ def setup
22
+ self.class.classes = CLASSES
23
+ end
24
+
25
+ def test_setup
26
+ testing_with do
27
+ assert_not_nil @klass_info[:update]
28
+ end
29
+ end
30
+
31
+ def test_update_attributes
32
+ testing_with do
33
+ assert @first.update_attributes(@klass_info[:update])
34
+ assert @first.reload
35
+ @klass_info[:update].each_pair do |attr_name, new_value|
36
+ assert_equal new_value, @first[attr_name], "Attribute #{attr_name} is incorrect"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/seat'
3
+
4
+ class TestValidations < ActiveSupport::TestCase
5
+ fixtures :seats
6
+
7
+ def test_uniqueness_validation_on_saved_record
8
+ s = Seat.find([1,1])
9
+ assert s.valid?
10
+ end
11
+ end
@@ -0,0 +1,195 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <link rel="stylesheet" href="stylesheets/screen.css" type="text/css" media="screen" />
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7
+ <title>
8
+ Composite Primary Keys
9
+ </title>
10
+ <script src="javascripts/rounded_corners_lite.inc.js" type="text/javascript"></script>
11
+ <style>
12
+
13
+ </style>
14
+ <script type="text/javascript">
15
+ window.onload = function() {
16
+ settings = {
17
+ tl: { radius: 10 },
18
+ tr: { radius: 10 },
19
+ bl: { radius: 10 },
20
+ br: { radius: 10 },
21
+ antiAlias: true,
22
+ autoPad: true,
23
+ validTags: ["div"]
24
+ }
25
+ var versionBox = new curvyCorners(settings, document.getElementById("version"));
26
+ versionBox.applyCornersToAll();
27
+ }
28
+ </script>
29
+ </head>
30
+ <body>
31
+ <div id="main">
32
+
33
+ <h1>Composite Primary Keys</h1>
34
+ <div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/compositekeys"; return false'>
35
+ Get Version
36
+ <a href="http://rubyforge.org/projects/compositekeys" class="numbers">2.3.5.1</a>
37
+ </div>
38
+ <h1>&#8594; Ruby on Rails</h1>
39
+ <h1>&#8594; ActiveRecords</h1>
40
+ <h2>What</h2>
41
+ <p>Ruby on Rails does not support composite primary keys. This free software is an extension <br />
42
+ to the database layer of Rails &#8211; <a href="http://wiki.rubyonrails.com/rails/pages/ActiveRecord">ActiveRecords</a> &#8211; to support composite primary keys as transparently as possible.</p>
43
+ <p>Any Ruby script using ActiveRecords can use Composite Primary Keys with this library.</p>
44
+ <h2>Installing</h2>
45
+ <p><pre class="syntax"><span class="ident">sudo</span> <span class="ident">gem</span> <span class="ident">install</span> <span class="ident">composite_primary_keys</span></pre></p>
46
+ <p>Rails: Add the following to the bottom of your <code>environment.rb</code> file</p>
47
+ <p><pre class="syntax"><span class="ident">require</span> <span class="punct">'</span><span class="string">composite_primary_keys</span><span class="punct">'</span></pre></p>
48
+ <p>Ruby scripts: Add the following to the top of your script</p>
49
+ <p><pre class="syntax"><span class="ident">require</span> <span class="punct">'</span><span class="string">rubygems</span><span class="punct">'</span>
50
+ <span class="ident">require</span> <span class="punct">'</span><span class="string">composite_primary_keys</span><span class="punct">'</span></pre></p>
51
+ <h2>The basics</h2>
52
+ <p>A model with composite primary keys would look like&#8230;</p>
53
+ <p><pre class="syntax"><span class="keyword">class </span><span class="class">Membership</span> <span class="punct">&lt;</span> <span class="constant">ActiveRecord</span><span class="punct">::</span><span class="constant">Base</span>
54
+ <span class="comment"># set_primary_keys *keys - turns on composite key functionality</span>
55
+ <span class="ident">set_primary_keys</span> <span class="symbol">:user_id</span><span class="punct">,</span> <span class="symbol">:group_id</span>
56
+ <span class="ident">belongs_to</span> <span class="symbol">:user</span>
57
+ <span class="ident">belongs_to</span> <span class="symbol">:group</span>
58
+ <span class="ident">has_many</span> <span class="symbol">:statuses</span><span class="punct">,</span> <span class="symbol">:class_name</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">MembershipStatus</span><span class="punct">',</span> <span class="symbol">:foreign_key</span> <span class="punct">=&gt;</span> <span class="punct">[</span><span class="symbol">:user_id</span><span class="punct">,</span> <span class="symbol">:group_id</span><span class="punct">]</span>
59
+ <span class="keyword">end</span></pre></p>
60
+ <p>A model associated with a composite key model would be defined like&#8230;</p>
61
+ <p><pre class="syntax"><span class="keyword">class </span><span class="class">MembershipStatus</span> <span class="punct">&lt;</span> <span class="constant">ActiveRecord</span><span class="punct">::</span><span class="constant">Base</span>
62
+ <span class="ident">belongs_to</span> <span class="symbol">:membership</span><span class="punct">,</span> <span class="symbol">:foreign_key</span> <span class="punct">=&gt;</span> <span class="punct">[</span><span class="symbol">:user_id</span><span class="punct">,</span> <span class="symbol">:group_id</span><span class="punct">]</span>
63
+ <span class="keyword">end</span></pre></p>
64
+ <p>That is, associations can include composite keys too. Nice.</p>
65
+ <h2>Demonstration of usage</h2>
66
+ <p>Once you&#8217;ve created your models to specify composite primary keys (such as the Membership class) and associations (such as MembershipStatus#membership), you can uses them like any normal model with associations.</p>
67
+ <p>But first, lets check out our primary keys.</p>
68
+ <p><pre class="syntax"><span class="constant">MembershipStatus</span><span class="punct">.</span><span class="ident">primary_key</span> <span class="comment"># =&gt; &quot;id&quot; # normal single key</span>
69
+ <span class="constant">Membership</span><span class="punct">.</span><span class="ident">primary_key</span> <span class="comment"># =&gt; [:user_id, :group_id] # composite keys</span>
70
+ <span class="constant">Membership</span><span class="punct">.</span><span class="ident">primary_key</span><span class="punct">.</span><span class="ident">to_s</span> <span class="comment"># =&gt; &quot;user_id,group_id&quot;</span></pre></p>
71
+ <p>Now we want to be able to find instances using the same syntax we always use for ActiveRecords&#8230;</p>
72
+ <p><pre class="syntax"><span class="constant">MembershipStatus</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="number">1</span><span class="punct">)</span> <span class="comment"># single id returns single instance</span>
73
+ <span class="punct">=&gt;</span> <span class="punct">&lt;</span><span class="constant">MembershipStatus</span><span class="punct">:</span><span class="number">0x392a8c8</span> <span class="attribute">@attributes</span><span class="punct">={&quot;</span><span class="string">id</span><span class="punct">&quot;=&gt;&quot;</span><span class="string">1</span><span class="punct">&quot;,</span> <span class="punct">&quot;</span><span class="string">status</span><span class="punct">&quot;=&gt;&quot;</span><span class="string">Active</span><span class="punct">&quot;}&gt;</span>
74
+ <span class="constant">Membership</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="number">1</span><span class="punct">,</span><span class="number">1</span><span class="punct">)</span> <span class="comment"># composite ids returns single instance</span>
75
+ <span class="punct">=&gt;</span> <span class="punct">&lt;</span><span class="constant">Membership</span><span class="punct">:</span><span class="number">0x39218b0</span> <span class="attribute">@attributes</span><span class="punct">={&quot;</span><span class="string">user_id</span><span class="punct">&quot;=&gt;&quot;</span><span class="string">1</span><span class="punct">&quot;,</span> <span class="punct">&quot;</span><span class="string">group_id</span><span class="punct">&quot;=&gt;&quot;</span><span class="string">1</span><span class="punct">&quot;}&gt;</span></pre></p>
76
+ <p>Using <a href="http://www.rubyonrails.org">Ruby on Rails</a>? You&#8217;ll want to your url_for helpers<br />
77
+ to convert composite keys into strings and back again&#8230;</p>
78
+ <p><pre class="syntax"><span class="constant">Membership</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="symbol">:first</span><span class="punct">).</span><span class="ident">to_param</span> <span class="comment"># =&gt; &quot;1,1&quot;</span></pre></p>
79
+ <p>And then use the string id within your controller to find the object again</p>
80
+ <p><pre class="syntax"><span class="ident">params</span><span class="punct">[</span><span class="symbol">:id</span><span class="punct">]</span> <span class="comment"># =&gt; '1,1'</span>
81
+ <span class="constant">Membership</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="ident">params</span><span class="punct">[</span><span class="symbol">:id</span><span class="punct">])</span>
82
+ <span class="punct">=&gt;</span> <span class="punct">&lt;</span><span class="constant">Membership</span><span class="punct">:</span><span class="number">0x3904288</span> <span class="attribute">@attributes</span><span class="punct">={&quot;</span><span class="string">user_id</span><span class="punct">&quot;=&gt;&quot;</span><span class="string">1</span><span class="punct">&quot;,</span> <span class="punct">&quot;</span><span class="string">group_id</span><span class="punct">&quot;=&gt;&quot;</span><span class="string">1</span><span class="punct">&quot;}&gt;</span></pre></p>
83
+ <p>That is, an ActiveRecord supporting composite keys behaves transparently<br />
84
+ throughout your application. Just like a normal ActiveRecord.</p>
85
+ <h2>Other tricks</h2>
86
+ <h3>Pass a list of composite ids to the <code>#find</code> method</h3>
87
+ <p><pre class="syntax"><span class="constant">Membership</span><span class="punct">.</span><span class="ident">find</span> <span class="punct">[</span><span class="number">1</span><span class="punct">,</span><span class="number">1</span><span class="punct">],</span> <span class="punct">[</span><span class="number">2</span><span class="punct">,</span><span class="number">1</span><span class="punct">]</span>
88
+ <span class="punct">=&gt;</span> <span class="punct">[</span>
89
+ <span class="punct">&lt;</span><span class="constant">Membership</span><span class="punct">:</span><span class="number">0x394ade8</span> <span class="attribute">@attributes</span><span class="punct">={&quot;</span><span class="string">user_id</span><span class="punct">&quot;=&gt;&quot;</span><span class="string">1</span><span class="punct">&quot;,</span> <span class="punct">&quot;</span><span class="string">group_id</span><span class="punct">&quot;=&gt;&quot;</span><span class="string">1</span><span class="punct">&quot;}&gt;,</span>
90
+ <span class="punct">&lt;</span><span class="constant">Membership</span><span class="punct">:</span><span class="number">0x394ada0</span> <span class="attribute">@attributes</span><span class="punct">={&quot;</span><span class="string">user_id</span><span class="punct">&quot;=&gt;&quot;</span><span class="string">2</span><span class="punct">&quot;,</span> <span class="punct">&quot;</span><span class="string">group_id</span><span class="punct">&quot;=&gt;&quot;</span><span class="string">1</span><span class="punct">&quot;}&gt;</span>
91
+ <span class="punct">]</span></pre></p>
92
+ <p>Perform <code>#count</code> operations</p>
93
+ <p><pre class="syntax"><span class="constant">MembershipStatus</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="symbol">:first</span><span class="punct">).</span><span class="ident">memberships</span><span class="punct">.</span><span class="ident">count</span> <span class="comment"># =&gt; 1</span></pre></p>
94
+ <h3>Routes with Rails</h3>
95
+ <p>From Pete Sumskas:</p>
96
+ <blockquote>
97
+ <p>I ran into one problem that I didn&#8217;t see mentioned on <a href="http://groups.google.com/group/compositekeys">this list</a> &#8211; <br />
98
+ and I didn&#8217;t see any information about what I should do to address it in the<br />
99
+ documentation (might have missed it).</p>
100
+ <p>The problem was that the urls being generated for a &#8216;show&#8217; action (for<br />
101
+ example) had a syntax like:</p>
102
+ <p><pre>/controller/show/123000,Bu70</pre></p>
103
+ <p>for a two-field composite PK. The default routing would not match that,<br />
104
+ so after working out how to do the routing I added:</p>
105
+ <p><pre class="syntax"><span class="ident">map</span><span class="punct">.</span><span class="ident">connect</span> <span class="punct">'</span><span class="string">:controller/:action/:id</span><span class="punct">',</span> <span class="symbol">:id</span> <span class="punct">=&gt;</span> <span class="punct">/</span><span class="regex"><span class="escape">\w</span>+(,<span class="escape">\w</span>+)*</span><span class="punct">/</span></pre></p>
106
+ <p>to my <code>route.rb</code> file.</p>
107
+ </blockquote>
108
+ <p><a name="dbs"></a></p>
109
+ <h2>Which databases?</h2>
110
+ <p>A suite of unit tests have been run on the following databases supported by ActiveRecord:</p>
111
+ <table>
112
+ <tr>
113
+ <th>Database</th>
114
+ <th>Test Success</th>
115
+ <th>User feedback</th>
116
+ </tr>
117
+ <tr>
118
+ <td>mysql </td>
119
+ <td><span class=success><span class="caps">YES</span></span></td>
120
+ <td><span class=success><span class="caps">YES</span></span> (<a href="mailto:compositekeys@googlegroups.com?subject=Mysql+is+working">Yes!</a> or <a href="mailto:compositekeys@googlegroups.com?subject=Mysql+is+failing">No&#8230;</a>)</td>
121
+ </tr>
122
+ <tr>
123
+ <td>sqlite3 </td>
124
+ <td><span class=success><span class="caps">YES</span></span></td>
125
+ <td><span class=success><span class="caps">YES</span></span> (<a href="mailto:compositekeys@googlegroups.com?subject=Sqlite3+is+working">Yes!</a> or <a href="mailto:compositekeys@googlegroups.com?subject=Sqlite3+is+failing">No&#8230;</a>)</td>
126
+ </tr>
127
+ <tr>
128
+ <td>postgresql</td>
129
+ <td><span class=success><span class="caps">YES</span></span></td>
130
+ <td><span class=success><span class="caps">YES</span></span> (<a href="mailto:compositekeys@googlegroups.com?subject=Postgresql+is+working">Yes!</a> or <a href="mailto:compositekeys@googlegroups.com?subject=Postgresql+is+failing">No&#8230;</a>)</td>
131
+ </tr>
132
+ <tr>
133
+ <td>oracle </td>
134
+ <td><span class=success><span class="caps">YES</span></span></td>
135
+ <td><span class=success><span class="caps">YES</span></span> (<a href="mailto:compositekeys@googlegroups.com?subject=Oracle+is+working">Yes!</a> or <a href="mailto:compositekeys@googlegroups.com?subject=Oracle+is+failing">No&#8230;</a>)</td>
136
+ </tr>
137
+ <tr>
138
+ <td>sqlserver </td>
139
+ <td><span class=unknown>???</span> (<a href="mailto:compositekeys@googlegroups.com?subject=Help+with+SQLServer">I can help</a>)</td>
140
+ <td><span class=unknown>???</span> (<a href="mailto:compositekeys@googlegroups.com?subject=SQLServer+is+working">Yes!</a> or <a href="mailto:compositekeys@googlegroups.com?subject=SQLServer+is+failing">No&#8230;</a>)</td>
141
+ </tr>
142
+ <tr>
143
+ <td>db2 </td>
144
+ <td><span class=unknown>???</span> (<a href="mailto:compositekeys@googlegroups.com?subject=Help+with+DB2">I can help</a>)</td>
145
+ <td><span class=unknown>???</span> (<a href="mailto:compositekeys@googlegroups.com?subject=DB2+is+working">Yes!</a> or <a href="mailto:compositekeys@googlegroups.com?subject=DB2+is+failing">No&#8230;</a>)</td>
146
+ </tr>
147
+ <tr>
148
+ <td>firebird </td>
149
+ <td><span class=unknown>???</span> (<a href="mailto:compositekeys@googlegroups.com?subject=Help+with+Firebird">I can help</a>)</td>
150
+ <td><span class=unknown>???</span> (<a href="mailto:compositekeys@googlegroups.com?subject=Firebird+is+working">Yes!</a> or <a href="mailto:compositekeys@googlegroups.com?subject=Firebird+is+failing">No&#8230;</a>)</td>
151
+ </tr>
152
+ <tr>
153
+ <td>sybase </td>
154
+ <td><span class=unknown>???</span> (<a href="mailto:compositekeys@googlegroups.com?subject=Help+with+Sybase">I can help</a>)</td>
155
+ <td><span class=unknown>???</span> (<a href="mailto:compositekeys@googlegroups.com?subject=Sybase+is+working">Yes!</a> or <a href="mailto:compositekeys@googlegroups.com?subject=Sybase+is+failing">No&#8230;</a>)</td>
156
+ </tr>
157
+ <tr>
158
+ <td>openbase </td>
159
+ <td><span class=unknown>???</span> (<a href="mailto:compositekeys@googlegroups.com?subject=Help+with+Openbase">I can help</a>)</td>
160
+ <td><span class=unknown>???</span> (<a href="mailto:compositekeys@googlegroups.com?subject=Openbase+is+working">Yes!</a> or <a href="mailto:compositekeys@googlegroups.com?subject=Openbase+is+failing">No&#8230;</a>)</td>
161
+ </tr>
162
+ <tr>
163
+ <td>frontbase </td>
164
+ <td><span class=unknown>???</span> (<a href="mailto:compositekeys@googlegroups.com?subject=Help+with+Frontbase">I can help</a>)</td>
165
+ <td><span class=unknown>???</span> (<a href="mailto:compositekeys@googlegroups.com?subject=Frontbase+is+working">Yes!</a> or <a href="mailto:compositekeys@googlegroups.com?subject=Frontbase+is+failing">No&#8230;</a>)</td>
166
+ </tr>
167
+ </table>
168
+ <h2>Dr Nic&#8217;s Blog</h2>
169
+ <p><a href="http://www.drnicwilliams.com">http://www.drnicwilliams.com</a> &#8211; for future announcements and<br />
170
+ other stories and things.</p>
171
+ <h2>Forum</h2>
172
+ <p><a href="http://groups.google.com/group/compositekeys">http://groups.google.com/group/compositekeys</a></p>
173
+ <h2>How to submit patches</h2>
174
+ <p>Read the <a href="http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/">8 steps for fixing other people&#8217;s code</a> and for section <a href="http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups">8b: Submit patch to Google Groups</a>, use the Google Group above.</p>
175
+ <p>The source for this project is available via git. You can <a href="http://github.com/drnic/composite_primary_keys/tree/master">browse and/or fork the source</a>, or to clone the project locally:</p>
176
+ <pre>git clone git://github.com/drnic/composite_primary_keys.git</pre>
177
+ <h2>Licence</h2>
178
+ <p>This code is free to use under the terms of the <span class="caps">MIT</span> licence.</p>
179
+ <h2>Contact</h2>
180
+ <p>Comments are welcome. Send an email to <a href="mailto:drnicwilliams@gmail.com">Dr Nic Williams</a>.</p>
181
+ <p class="coda">
182
+ <a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>, 13th February 2010<br>
183
+ Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
184
+ </p>
185
+ </div>
186
+
187
+ <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
188
+ </script>
189
+ <script type="text/javascript">
190
+ _uacct = "UA-567811-2";
191
+ urchinTracker();
192
+ </script>
193
+
194
+ </body>
195
+ </html>
data/website/index.txt ADDED
@@ -0,0 +1,159 @@
1
+ h1. Composite Primary Keys
2
+
3
+ h1. -> Ruby on Rails
4
+
5
+ h1. -> ActiveRecords
6
+
7
+ h2. What
8
+
9
+ Ruby on Rails does not support composite primary keys. This free software is an extension
10
+ to the database layer of Rails - "ActiveRecords":http://wiki.rubyonrails.com/rails/pages/ActiveRecord - to support composite primary keys as transparently as possible.
11
+
12
+ Any Ruby script using ActiveRecords can use Composite Primary Keys with this library.
13
+
14
+ h2. Installing
15
+
16
+ <pre syntax="ruby">sudo gem install composite_primary_keys</pre>
17
+
18
+ Rails: Add the following to the bottom of your <code>environment.rb</code> file
19
+
20
+ <pre syntax="ruby">require 'composite_primary_keys'</pre>
21
+
22
+ Ruby scripts: Add the following to the top of your script
23
+
24
+ <pre syntax="ruby">require 'rubygems'
25
+ require 'composite_primary_keys'</pre>
26
+
27
+ h2. The basics
28
+
29
+ A model with composite primary keys would look like...
30
+
31
+ <pre syntax="ruby">class Membership < ActiveRecord::Base
32
+ # set_primary_keys *keys - turns on composite key functionality
33
+ set_primary_keys :user_id, :group_id
34
+ belongs_to :user
35
+ belongs_to :group
36
+ has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
37
+ end</pre>
38
+
39
+ A model associated with a composite key model would be defined like...
40
+
41
+ <pre syntax="ruby">class MembershipStatus < ActiveRecord::Base
42
+ belongs_to :membership, :foreign_key => [:user_id, :group_id]
43
+ end</pre>
44
+
45
+ That is, associations can include composite keys too. Nice.
46
+
47
+ h2. Demonstration of usage
48
+
49
+ Once you've created your models to specify composite primary keys (such as the Membership class) and associations (such as MembershipStatus#membership), you can uses them like any normal model with associations.
50
+
51
+ But first, lets check out our primary keys.
52
+
53
+ <pre syntax="ruby">MembershipStatus.primary_key # => "id" # normal single key
54
+ Membership.primary_key # => [:user_id, :group_id] # composite keys
55
+ Membership.primary_key.to_s # => "user_id,group_id"</pre>
56
+
57
+ Now we want to be able to find instances using the same syntax we always use for ActiveRecords...
58
+
59
+ <pre syntax="ruby">MembershipStatus.find(1) # single id returns single instance
60
+ => <MembershipStatus:0x392a8c8 @attributes={"id"=>"1", "status"=>"Active"}>
61
+ Membership.find(1,1) # composite ids returns single instance
62
+ => <Membership:0x39218b0 @attributes={"user_id"=>"1", "group_id"=>"1"}></pre>
63
+
64
+ Using "Ruby on Rails":http://www.rubyonrails.org? You'll want to your url_for helpers
65
+ to convert composite keys into strings and back again...
66
+
67
+ <pre syntax="ruby">Membership.find(:first).to_param # => "1,1"</pre>
68
+
69
+ And then use the string id within your controller to find the object again
70
+
71
+ <pre syntax="ruby">params[:id] # => '1,1'
72
+ Membership.find(params[:id])
73
+ => <Membership:0x3904288 @attributes={"user_id"=>"1", "group_id"=>"1"}></pre>
74
+
75
+ That is, an ActiveRecord supporting composite keys behaves transparently
76
+ throughout your application. Just like a normal ActiveRecord.
77
+
78
+
79
+ h2. Other tricks
80
+
81
+ h3. Pass a list of composite ids to the <code>#find</code> method
82
+
83
+ <pre syntax="ruby">Membership.find [1,1], [2,1]
84
+ => [
85
+ <Membership:0x394ade8 @attributes={"user_id"=>"1", "group_id"=>"1"}>,
86
+ <Membership:0x394ada0 @attributes={"user_id"=>"2", "group_id"=>"1"}>
87
+ ]</pre>
88
+
89
+ Perform <code>#count</code> operations
90
+
91
+ <pre syntax="ruby">MembershipStatus.find(:first).memberships.count # => 1</pre>
92
+
93
+ h3. Routes with Rails
94
+
95
+ From Pete Sumskas:
96
+
97
+ <blockquote>
98
+ I ran into one problem that I didn't see mentioned on "this list":http://groups.google.com/group/compositekeys -
99
+ and I didn't see any information about what I should do to address it in the
100
+ documentation (might have missed it).
101
+
102
+ The problem was that the urls being generated for a 'show' action (for
103
+ example) had a syntax like:
104
+
105
+ <pre>/controller/show/123000,Bu70</pre>
106
+
107
+ for a two-field composite PK. The default routing would not match that,
108
+ so after working out how to do the routing I added:
109
+
110
+ <pre syntax="ruby">map.connect ':controller/:action/:id', :id => /\w+(,\w+)*/</pre>
111
+
112
+ to my <code>route.rb</code> file.
113
+
114
+ </blockquote>
115
+
116
+ <a name="dbs"></a>
117
+
118
+ h2. Which databases?
119
+
120
+
121
+ A suite of unit tests have been run on the following databases supported by ActiveRecord:
122
+
123
+ |_.Database|_.Test Success|_.User feedback|
124
+ |mysql |<span class=success>YES</span>|<span class=success>YES</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Mysql+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Mysql+is+failing)|
125
+ |sqlite3 |<span class=success>YES</span>|<span class=success>YES</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Sqlite3+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Sqlite3+is+failing)|
126
+ |postgresql|<span class=success>YES</span>|<span class=success>YES</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Postgresql+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Postgresql+is+failing)|
127
+ |oracle |<span class=success>YES</span>|<span class=success>YES</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Oracle+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Oracle+is+failing)|
128
+ |sqlserver |<span class=unknown>???</span> ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+SQLServer)|<span class=unknown>???</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=SQLServer+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=SQLServer+is+failing)|
129
+ |db2 |<span class=unknown>???</span> ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+DB2)|<span class=unknown>???</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=DB2+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=DB2+is+failing)|
130
+ |firebird |<span class=unknown>???</span> ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+Firebird)|<span class=unknown>???</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Firebird+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Firebird+is+failing)|
131
+ |sybase |<span class=unknown>???</span> ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+Sybase)|<span class=unknown>???</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Sybase+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Sybase+is+failing)|
132
+ |openbase |<span class=unknown>???</span> ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+Openbase)|<span class=unknown>???</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Openbase+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Openbase+is+failing)|
133
+ |frontbase |<span class=unknown>???</span> ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+Frontbase)|<span class=unknown>???</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Frontbase+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Frontbase+is+failing)|
134
+
135
+ h2. Dr Nic's Blog
136
+
137
+ "http://www.drnicwilliams.com":http://www.drnicwilliams.com - for future announcements and
138
+ other stories and things.
139
+
140
+ h2. Forum
141
+
142
+ "http://groups.google.com/group/compositekeys":http://groups.google.com/group/compositekeys
143
+
144
+ h2. How to submit patches
145
+
146
+ Read the "8 steps for fixing other people's code":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/ and for section "8b: Submit patch to Google Groups":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups, use the Google Group above.
147
+
148
+
149
+ The source for this project is available via git. You can "browse and/or fork the source":http://github.com/drnic/composite_primary_keys/tree/master, or to clone the project locally:
150
+
151
+ <pre>git clone git://github.com/drnic/composite_primary_keys.git</pre>
152
+
153
+ h2. Licence
154
+
155
+ This code is free to use under the terms of the MIT licence.
156
+
157
+ h2. Contact
158
+
159
+ Comments are welcome. Send an email to "Dr Nic Williams":mailto:drnicwilliams@gmail.com.