<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: model code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 05:50:57 GMT</pubDate>
    <description>DZone Snippets: model code</description>
    <item>
      <title>go from model to associated table name and back</title>
      <link>http://snippets.dzone.com/posts/show/2135</link>
      <description>Given a table object, it returns the related string object; e.g. SubAttribute =&gt; 'sub-attribute'.  Useful if you want to make a list of all your tables with perhaps their fields listed out to the side.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def stringify_table( table, replace_char = '-', pluralize = false )&lt;br /&gt;  string = table.to_s.gsub( /([A-Za-z])([A-Z])/, '\1' &lt;&lt; replace_char.to_s &lt;&lt; '\2' )&lt;br /&gt;  string = string.pluralize if pluralize&lt;br /&gt;  string&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Given a string akin to the name of a table, it returns the related table object; e.g. 'sub_attributes' =&gt; SubAttribute.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def tablify_string( string )&lt;br /&gt;  eval( string.to_s.gsub( /_id/, '' ).singularize.split( '_' ).collect { |word| word.capitalize }.join )&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 03 Jun 2006 00:50:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2135</guid>
      <author>moneypenny ()</author>
    </item>
  </channel>
</rss>
