<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://mirmodynamics.com/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>Mirmo Dynamics - Tag - rake</title>
  <link>http://mirmodynamics.com/</link>
  <atom:link href="http://mirmodynamics.com/feed/tag/rake/rss2" rel="self" type="application/rss+xml"/>
  <description>Si tu kiffes pas reunoi, t'écoutes pas et puis c'est tout.</description>
  <language>en</language>
  <pubDate>Sun, 14 Mar 2010 19:59:01 +0100</pubDate>
  <copyright>2003-2009 &amp;copy; Geoffrey Bachelet</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>yaml, activerecord and acts_as_nested_set</title>
    <link>http://mirmodynamics.com/post/2007/12/02/yaml-activerecord-and-acts_as_nested_set</link>
    <guid isPermaLink="false">urn:md5:8723be74d366c359a8c02a308d5844be</guid>
    <pubDate>Sun, 02 Dec 2007 16:41:00 +0100</pubDate>
    <dc:creator>Geoffrey</dc:creator>
        <category>Coding</category>
        <category>activerecord</category><category>rails</category><category>rake</category><category>ruby</category><category>yaml</category>    
    <description>    &lt;p&gt;I used to use this &lt;a href=&quot;http://thecaribbeanweblog.com/index.php/2007/06/21/150-yaml-et-activerecord-et-sql-plus-generalement&quot;&gt;yaml_to_ar lib&lt;/a&gt; from &lt;a href=&quot;http://thecaribbeanweblog.com/&quot;&gt;christophe&lt;/a&gt; to load categories tree into my database, using &lt;code&gt;&lt;a href=&quot;http://wiki.rubyonrails.org/rails/pages/ActsAsTree&quot;&gt;acts_as_tree&lt;/a&gt;&lt;/code&gt; in the model that was perfect. Arrived the time when I felt the need to use &lt;code&gt;&lt;a href=&quot;http://api.rubyonrails.com/classes/ActiveRecord/Acts/NestedSet/ClassMethods.html&quot;&gt;acts_as_nested_set&lt;/a&gt;&lt;/code&gt; instead, for which I had to fill the &lt;code&gt;lft&lt;/code&gt; and &lt;code&gt;rgt&lt;/code&gt; columns. So I just rewrote the &lt;em&gt;yaml_to_ar&lt;/em&gt; piece of code (put this in &lt;code&gt;lib/yaml_to_ar.rb&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;
[ruby]
require 'yaml'

class YAML_to_AR

  def initialize(file, model)
    @data = File.open(file) { |yf| YAML::load( yf ) }
    @model = model
  end

  def process(data = @data, parent = nil)
    if data.is_a? Array
      data.each do |val|
        process(val, parent)
      end
    elsif data.is_a? Hash
      data.each do |key,val|
        parent = @model.create(:title =&amp;gt; key)
        process(val, parent)
      end
    elsif data.is_a? String
      parent.add_child(@model.create(:title =&amp;gt; data))
    end
  end

 end
&lt;/pre&gt;


&lt;p&gt;This should handle both &lt;code&gt;acts_as_tree&lt;/code&gt; and &lt;code&gt;acts_as_nested&lt;/code&gt;. To ease things a bit further, I also wrote a rake task (to drop in &lt;code&gt;lib/tasks/db_load_categories.rake&lt;/code&gt; for example):&lt;/p&gt;

&lt;pre&gt;
[ruby]
namespace :db do
  desc &amp;quot;Loads categories defaults data&amp;quot;
  task :load_categories =&amp;gt; :environment do
    require 'lib/yaml_to_ar'
    Category.delete_all
    categories = YAML_to_AR.new('db/categories.yml', Category)
    categories.process
  end
end
&lt;/pre&gt;


&lt;p&gt;Now I just &lt;code&gt;rake db:load_categories&lt;/code&gt;, and voila !&lt;/p&gt;</description>
    
    
    
          <comments>http://mirmodynamics.com/post/2007/12/02/yaml-activerecord-and-acts_as_nested_set#comment-form</comments>
      <wfw:comment>http://mirmodynamics.com/post/2007/12/02/yaml-activerecord-and-acts_as_nested_set#comment-form</wfw:comment>
      <wfw:commentRss>http://mirmodynamics.com/feed/atom/comments/1077</wfw:commentRss>
      </item>
    
</channel>
</rss>