Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7772 (closed enhancement: duplicate)

Opened 3 years ago

Last modified 3 years ago

[PATCH] reference foreign key lookups, parent_ids, and auto_increment in fixtures

Reported by: nfbuckley Assigned to: core
Priority: normal Milestone: 1.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc: bitsweat

Description

In response to ticket #6424 I have included a patch to implement foreign key references as suggested by esad. Basically, this patch allows you to do the following:

parent table

robinson:
  id: 1
  name: Robinson

child table

joe_robinson:
  id: 1
  family_id: <%= @robinson['id'] %>
  name: Joe Robinson

There is of course the requirement that parent tables be listed first when creating fixtures.

In addition, there is the ability to reference parent records via the new auto_increment, push_parent, parent, and pop_parent methods. The method auto_increment, as expected, keeps a running counter. The parent methods use the counter and ancestor stack to keep track of parents. Here is an example:

--- !!omap
- grandpa_joe_robinson:
    id: <%= auto_increment %>
    family_id: <%= @robinson['id'] %>
    name: Joe Robinson

<% push_parent %>
- father_joe_robinson:
    id: <%= auto_increment %>
    family_id: <%= @robinson['id'] %>
    parent_id: <%= parent %>
    name: Joe Robinson Jr.

<% push_parent %>
- grandson_joe_robinson:
    id: <%= auto_increment %>
    family_id: <%= @robinson['id'] %>
    parent_id: <%= parent %>
    name: Joe Robinson III

<% pop_parent %>
- uncle_john_robinson:
    id: <%= auto_increment %>
    family_id: <%= @robinson['id'] %>
    parent_id: <%= parent %>
    name: John Robinson

The auto_increment methods can be used anywhere, but the parent methods are only practical using omap. For this reason, I had to include in the patch a fix for omap, which is currently broken as mentioned in #2665.

Attachments

fixtures_binding.diff (9.0 kB) - added by nfbuckley on 03/09/07 01:07:10.
patch for fixtures

Change History

03/09/07 01:07:10 changed by nfbuckley

  • attachment fixtures_binding.diff added.

patch for fixtures

03/09/07 01:09:53 changed by nfbuckley

  • type changed from defect to enhancement.

03/09/07 01:17:05 changed by bitsweat

  • cc set to bitsweat.

Could you attach the bare omap fix + tests to #2665? Thanks!

03/09/07 01:18:06 changed by bitsweat

  • status changed from new to closed.
  • resolution set to duplicate.

Also, please continue work in #6424 rather than creating new tickets for the same topic! Thanks :-)