composite_primary_keys 3.0.3 → 3.0.4
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.
- data/History.txt +7 -1
- data/lib/composite_primary_keys/base.rb +1 -1
- data/lib/composite_primary_keys/finder_methods.rb +18 -6
- data/lib/composite_primary_keys/relation.rb +10 -3
- data/lib/composite_primary_keys/version.rb +1 -1
- data/test/debug.log +165 -0
- data/test/fixtures/product.rb +1 -1
- data/test/test_delete.rb +30 -17
- data/test/test_ids.rb +1 -0
- metadata +4 -4
data/History.txt
CHANGED
@@ -1,4 +1,10 @@
|
|
1
|
-
== 3.0.
|
1
|
+
== 3.0.4 2010-11-21
|
2
|
+
* Support deleting records when dependent is set to :delete_all
|
3
|
+
* Switch the method #id to return a CompositeKeys instance (thus going back to how
|
4
|
+
this was done in previous CPK versions)
|
5
|
+
|
6
|
+
|
7
|
+
== 3.0.3 2010-11-19
|
2
8
|
* Support ActiveRecord 3.0.3 and Arel 2+
|
3
9
|
* Require ActiveRecord 3.0.3 as minimum dependency
|
4
10
|
* Fix double quoting issue with table names - thanks to Kevin Motschiedler for a patch.
|
@@ -96,7 +96,7 @@ module ActiveRecord
|
|
96
96
|
# whether you name it the default 'id' or set it to something else.
|
97
97
|
def id
|
98
98
|
attr_names = self.class.primary_keys
|
99
|
-
|
99
|
+
::CompositePrimaryKeys::CompositeKeys.new(attr_names.map { |attr_name| read_attribute(attr_name) })
|
100
100
|
end
|
101
101
|
alias_method :ids, :id
|
102
102
|
|
@@ -40,23 +40,35 @@ module CompositePrimaryKeys
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def exists?(id = nil)
|
43
|
+
# ID can be:
|
44
|
+
# Array - ['department_id = ? and location_id = ?', 1, 1]
|
45
|
+
# Array -> [1,2]
|
46
|
+
# CompositeKeys -> [1,2]
|
47
|
+
|
48
|
+
id = id.id if ::ActiveRecord::Base === id
|
49
|
+
|
43
50
|
case id
|
51
|
+
# CPK
|
52
|
+
when CompositePrimaryKeys::CompositeKeys
|
53
|
+
relation = select(primary_keys).limit(1)
|
54
|
+
relation = relation.where(ids_predicate(id)) if id
|
55
|
+
relation.first ? true : false
|
44
56
|
when Array
|
45
57
|
# CPK
|
46
58
|
if id.first.is_a?(String) and id.first.match(/\?/)
|
47
59
|
where(id).exists?
|
48
60
|
else
|
49
|
-
|
61
|
+
exists?(id.to_composite_keys)
|
50
62
|
end
|
51
63
|
when Hash
|
52
64
|
where(id).exists?
|
53
65
|
else
|
54
|
-
|
55
|
-
|
56
|
-
|
66
|
+
# CPK
|
67
|
+
#relation = select(primary_key).limit(1)
|
68
|
+
#relation = relation.where(primary_key.eq(id)) if id
|
69
|
+
relation = select(primary_keys).limit(1)
|
70
|
+
relation = relation.where(ids_predicate(id)) if id
|
57
71
|
|
58
|
-
relation = select(primary_keys).limit(1)
|
59
|
-
relation = relation.where(ids_predicate(id)) if id
|
60
72
|
relation.first ? true : false
|
61
73
|
end
|
62
74
|
end
|
@@ -4,9 +4,16 @@ module CompositePrimaryKeys
|
|
4
4
|
module InstanceMethods
|
5
5
|
def ids_predicate(id)
|
6
6
|
predicate = nil
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
|
8
|
+
if id.kind_of?(CompositePrimaryKeys::CompositeKeys)
|
9
|
+
id = [id]
|
10
|
+
end
|
11
|
+
|
12
|
+
id.each do |composite_id|
|
13
|
+
self.primary_keys.zip(composite_id).each do |key, value|
|
14
|
+
eq = table[key].eq(value)
|
15
|
+
predicate = predicate ? predicate.and(eq) : eq
|
16
|
+
end
|
10
17
|
end
|
11
18
|
predicate
|
12
19
|
end
|
data/test/debug.log
CHANGED
@@ -285,3 +285,168 @@ HINT: No operator matches the given name and argument type(s). You might need t
|
|
285
285
|
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
286
286
|
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
287
287
|
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
288
|
+
[1m[36mSQL (29.0ms)[0m [1mSHOW client_min_messages[0m
|
289
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
290
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
291
|
+
[1m[35mSQL (1.0ms)[0m SET client_min_messages TO 'notice'
|
292
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
293
|
+
PGError: ERROR: null value in column "product_id" violates not-null constraint
|
294
|
+
: UPDATE "product_tariffs" SET "product_id" = NULL WHERE "product_tariffs"."product_id" = 1 AND "product_tariffs"."product_id" = 1 AND "product_tariffs"."tariff_id" = 2 AND "product_tariffs"."tariff_start_date" = '2010-11-20'
|
295
|
+
[1m[36mSQL (1.0ms)[0m [1mSHOW client_min_messages[0m
|
296
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
297
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
298
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
299
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
300
|
+
[1m[36mSQL (1.0ms)[0m [1mSHOW client_min_messages[0m
|
301
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
302
|
+
[1m[36mSQL (1.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
303
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
304
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
305
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
306
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
307
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
308
|
+
[1m[35mSQL (15.6ms)[0m SET client_min_messages TO 'notice'
|
309
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
310
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
311
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
312
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
313
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
314
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
315
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
316
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
317
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
318
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
319
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
320
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
321
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
322
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
323
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
324
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
325
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
326
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
327
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
328
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
329
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
330
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
331
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
332
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
333
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
334
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
335
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
336
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
337
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
338
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
339
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
340
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
341
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
342
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
343
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
344
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
345
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
346
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
347
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
348
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
349
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
350
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
351
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
352
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
353
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
354
|
+
[1m[36mSQL (15.6ms)[0m [1mSHOW TIME ZONE[0m
|
355
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
356
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
357
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
358
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
359
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
360
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
361
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
362
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
363
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
364
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
365
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
366
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
367
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
368
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
369
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
370
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
371
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
372
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
373
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
374
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
375
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
376
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
377
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
378
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
379
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
380
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
381
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
382
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
383
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
384
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
385
|
+
PGError: ERROR: column "tariff_idstart_date" does not exist
|
386
|
+
LINE 1: SELECT COUNT(DISTINCT tariff_idstart_date) FROM "tariffs" LE...
|
387
|
+
^
|
388
|
+
: SELECT COUNT(DISTINCT tariff_idstart_date) FROM "tariffs" LEFT OUTER JOIN "product_tariffs" ON "product_tariffs"."tariff_id" = "tariffs"."tariff_id" AND "product_tariffs"."tariff_start_date" = "tariffs"."start_date"
|
389
|
+
PGError: ERROR: operator does not exist: character varying = integer
|
390
|
+
LINE 1: ...ments".person_type = 'User') AND ("comments".person_id = 1))
|
391
|
+
^
|
392
|
+
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
|
393
|
+
: SELECT "hacks".* FROM "hacks" INNER JOIN "comments" ON ("hacks"."name" = "comments"."hack_id") WHERE (("comments".person_type = 'User') AND ("comments".person_id = 1))
|
394
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
395
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
396
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
397
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
398
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
399
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
400
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
401
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
402
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
403
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
404
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
405
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
406
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
407
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
408
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
409
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
410
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
411
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
412
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
413
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
414
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
415
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
416
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
417
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
418
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
419
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
420
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
421
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
422
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
423
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
424
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
425
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
426
|
+
[1m[36mSQL (15.6ms)[0m [1mSET standard_conforming_strings = on[0m
|
427
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
428
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
429
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
430
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
431
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
432
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
433
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
434
|
+
PGError: ERROR: column "tariff_idstart_date" does not exist
|
435
|
+
LINE 1: SELECT COUNT(DISTINCT tariff_idstart_date) FROM "tariffs" LE...
|
436
|
+
^
|
437
|
+
: SELECT COUNT(DISTINCT tariff_idstart_date) FROM "tariffs" LEFT OUTER JOIN "product_tariffs" ON "product_tariffs"."tariff_id" = "tariffs"."tariff_id" AND "product_tariffs"."tariff_start_date" = "tariffs"."start_date"
|
438
|
+
PGError: ERROR: operator does not exist: character varying = integer
|
439
|
+
LINE 1: ...ments".person_type = 'User') AND ("comments".person_id = 1))
|
440
|
+
^
|
441
|
+
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
|
442
|
+
: SELECT "hacks".* FROM "hacks" INNER JOIN "comments" ON ("hacks"."name" = "comments"."hack_id") WHERE (("comments".person_type = 'User') AND ("comments".person_id = 1))
|
443
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
444
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
445
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
446
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
447
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
448
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW client_min_messages[0m
|
449
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'panic'
|
450
|
+
[1m[36mSQL (0.0ms)[0m [1mSET standard_conforming_strings = on[0m
|
451
|
+
[1m[35mSQL (0.0ms)[0m SET client_min_messages TO 'notice'
|
452
|
+
[1m[36mSQL (0.0ms)[0m [1mSHOW TIME ZONE[0m
|
data/test/fixtures/product.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
class Product < ActiveRecord::Base
|
2
2
|
set_primary_keys :id # redundant
|
3
|
-
has_many :product_tariffs, :foreign_key => :product_id
|
3
|
+
has_many :product_tariffs, :foreign_key => :product_id, :dependent => :delete_all
|
4
4
|
has_one :product_tariff, :foreign_key => :product_id
|
5
5
|
|
6
6
|
has_many :tariffs, :through => :product_tariffs, :foreign_key => [:tariff_id, :tariff_start_date]
|
data/test/test_delete.rb
CHANGED
@@ -27,26 +27,26 @@ class TestDelete < ActiveSupport::TestCase
|
|
27
27
|
assert @first.destroy
|
28
28
|
end
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def test_destroy_one_alone_via_class
|
32
32
|
testing_with do
|
33
33
|
assert @klass.destroy(@first.id)
|
34
34
|
end
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
def test_delete_one_alone
|
38
38
|
testing_with do
|
39
39
|
assert @klass.delete(@first.id)
|
40
40
|
end
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
def test_delete_many
|
44
44
|
testing_with do
|
45
45
|
to_delete = @klass.find(:all)[0..1]
|
46
46
|
assert_equal 2, to_delete.length
|
47
47
|
end
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
def test_delete_all
|
51
51
|
testing_with do
|
52
52
|
@klass.delete_all
|
@@ -63,21 +63,34 @@ class TestDelete < ActiveSupport::TestCase
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def test_delete_association
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
66
|
+
department = Department.find(1,1)
|
67
|
+
assert_equal 2, department.employees.size , "Before delete employee count should be 2."
|
68
|
+
first_employee = department.employees[0]
|
69
|
+
department.employees.delete(first_employee)
|
70
|
+
assert_equal 1, department.employees.size, "After delete employee count should be 1."
|
71
|
+
department.reload
|
72
|
+
assert_equal 1, department.employees.size, "After delete and a reload from DB employee count should be 1."
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_delete_cpk_all_association
|
76
|
+
# In this case the association is a has_many composite key with
|
77
|
+
# dependent set to :delete_all
|
78
|
+
product = Product.find(1)
|
79
|
+
assert_equal(2, product.product_tariffs.length)
|
80
|
+
|
81
|
+
product_tariff = product.product_tariffs.first
|
82
|
+
product.product_tariffs.delete(product_tariff)
|
83
|
+
|
84
|
+
product.reload
|
85
|
+
assert_equal(1, product.product_tariffs.length)
|
73
86
|
end
|
74
87
|
|
75
88
|
def test_delete_records_for_has_many_association_with_composite_primary_key
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
89
|
+
reference_type = ReferenceType.find(1)
|
90
|
+
codes_to_delete = reference_type.reference_codes[0..1]
|
91
|
+
assert_equal 3, reference_type.reference_codes.size, "Before deleting records reference_code count should be 3."
|
92
|
+
reference_type.reference_codes.delete_records(codes_to_delete)
|
93
|
+
reference_type.reload
|
94
|
+
assert_equal 1, reference_type.reference_codes.size, "After deleting 2 records and a reload from DB reference_code count should be 1."
|
82
95
|
end
|
83
96
|
end
|
data/test/test_ids.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composite_primary_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 3
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 3.0.
|
9
|
+
- 4
|
10
|
+
version: 3.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dr Nic Williams
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-11-
|
19
|
+
date: 2010-11-21 00:00:00 -07:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|