[Rails] AR: How to store and restore in YML.

Duane Johnson duane.johnson at gmail.com
Mon Jan 2 00:55:34 GMT 2006


On Jan 1, 2006, at 5:20 PM, Jake Janovetz wrote:

> Hello-
>
> I have a database with entries that are segmented by customers.  I  
> would
> like to be able to store / delete / restore a particular customer's
> information.  I will need to rebuild the rows on restore and also
> rebuild the associations for the different tables.
>
> For example,
> customer {
>   id
>   name
> }
> companies {
>   id
>   customer_id    (belongs_to)
>   name
> }
> contacts {
>   id
>   company_id     (belongs_to)
>   name
> }
>
>
> So, if I just write out YML for the rows in this database, I'd end up
> with something like:
> Customer: id=34, name="Bob"
> Company: id=134, customer_id=34, name="Intel"
> Company: id=149, customer_id=34, name="Apple"
> Contact: id=9383, company_id=134, name="Jerry"
> Contact: id=8392, company_id=149, name="Robert"
>
> The problem with this is that it is more difficult to regenerate those
> rows because the associations would need to be broken and rebuilt.
>
> What I would like to do is use the names in the YML output so I can
> create records in a valid order, and do "finds" to find the right
> records for associations. (Uniqueness is enforced so a find will  
> find a
> single record)
>
> Customer: id=34, name="Bob"
> Company: id=134, customer_id="Bob", name="Intel"
> Company: id=149, customer_id="Bob", name="Apple"
> Contact: id=9383, company_id="Intel", name="Jerry"
> Contact: id=8392, company_id="Apple", name="Robert"
>
> I am interested in hearing any rails/ruby tricks that would help me do
> this for a number of different tables in the database.
>
> Any suggestions?
>
Have you taken a look at the way ActiveRecord does ordered fixtures?   
It uses YAML::Omap to ensure that the hashes are each inserted in  
their proper order.

http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/classes/YAML/Omap.html

Duane Johnson
(canadaduane)
http://blog.inquirylabs.com/


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060102/c3ef09f6/attachment.html


More information about the Rails mailing list