# What's a good way to specify set of records to be created across multiple models (with associations)

**URL:** <https://www.ruby-forum.com/t/whats-a-good-way-to-specify-set-of-records-to-be-created-across-multiple-models-with-associations/157482>\
**Category:** Rails\
**Created:** [January 12, 2009, 7:11am UTC](https://www.ruby-forum.com/t/whats-a-good-way-to-specify-set-of-records-to-be-created-across-multiple-models-with-associations/157482 "2009-01-12T07:11:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 12, 2009, 7:11am UTC](https://www.ruby-forum.com/t/whats-a-good-way-to-specify-set-of-records-to-be-created-across-multiple-models-with-associations/157482/1 "2009-01-12T07:11:12Z")

</div>

Hi,

I need to be able to specify for my application, via a configuration  
approach (i.e. power user, no nice UI required), a way to automate  
creation  
of a pre-determine set of database records across a few models. So  
I’ll  
try to explain as best I can:

- Assume I have a model hierarchy something like the following  
(basically  
allocating a back account item out):

- model “Bank Account Item” can have multiple
- model "Allocation"s and within each allocation it defines  
.amount (for the allocation, total for each allocation needs to equal  
bank\_account)  
.person\_id (link to existing person in the Person table/model)  
.category\_id (link to existing category in the Category table/model)

- There will be an automation routine that will say, “if you see a bank  
account item with a description of XYZ, then automatically set up it’s  
allocation to People & Categories” (i.e. predefined). This could be as  
simple as an “Automation” model that has a regex that would be used, and  
then a string to store the association(s) that should be created (e.g.  
XML,  
or YAML, or Ruby code, i.e. the reason for the question).

- An example for what this string might represent for say an automation  
record where the regex was “Telephone Bill” might be:  
--------example---------

- Association 1

- amount = 30% (calculates absolute amount on the fly)
- person\_id = 1 \<Person A’s id\>
- category\_id = 23 

- Association 2

- amount = 70% (calculates absolute amount on the fly)
- person\_id = 1 \<Person A’s id\>
- category\_id = 3   
--------example---------

Any suggestions re (a) a nice generic way to do this and (b) what format  
may  
be the best (e.g. XML, Ruby code, YAML).

Some ideas that come to mind:  
[1] Ruby code that takes input (not great)  
[2] Ruby code that describes “example” above in the form of Array or  
Hashes.  
Maybe best?  
[3] XML - readable, but a little more code to import?  
[4] YAML - not that readable, but easy to import  
[4] Other?

thanks

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 12, 2009, 12:04pm UTC](https://www.ruby-forum.com/t/whats-a-good-way-to-specify-set-of-records-to-be-created-across-multiple-models-with-associations/157482/2 "2009-01-12T12:04:31Z")

</div>

On Mon, Jan 12, 2009 at 5:10 PM, Greg H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need to be able to specify for my application, via a configuration  
> approach (i.e. power user, no nice UI required), a way to automate creation  
> of a pre-determine set of database records across a few models. So I’ll  
> try to explain as best I can:

Can’t remember the library names… but you want to look for a  
“factory” of some sort.

–

> **[Mikel Lindsaar](https://lindsaar.net/)**
>
> Internet Home for Mikel Lindsaar

Rails, RSpec and Life blog…

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 12, 2009, 9:05pm UTC](https://www.ruby-forum.com/t/whats-a-good-way-to-specify-set-of-records-to-be-created-across-multiple-models-with-associations/157482/3 "2009-01-12T21:05:20Z")

</div>

I’m more interested in how the routine (albeit in a Factory or not  
works)  
actually - e.g. best way of storing the info: xml, ruby code, yaml??

On Mon, Jan 12, 2009 at 9:04 PM, Mikel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> “factory” of some sort.
> 
> –  
> [http://lindsaar.net/](http://lindsaar.net/)  
> Rails, RSpec and Life blog…
> 
> >

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)
