Rails 4.0 先睹为快:扩展ActiveRecord支持PostgreSQL的数据类型
openkk 12年前
本周,我的一个功能请求已经被接受到下一个版本的 Rails 中,这就是增加对 PostgreSQL 数据库的 MACADDR、INET 和 CIDR 数据类型的支持,也就是说在 Rails 4.0 中你可以使用如下代码: <pre class="brush:ruby; toolbar: true; auto-links: false;">create_table :network_types do |t| t.cidr :cidr_address t.inet :ip_address t.macaddr :mac_address end</pre> <p>同时,数据库模型的生成也支持这三个数据类型,而之前它们都被当成是字符串来处理。</p> <p>ActiveRecord 将自动的把 INET 和 CIDR 类型转成 Ruby 的 IPAddr,而 MACADDR 将转成字符串类型。</p> <article> <p>This week, I had a <a href="/misc/goto?guid=4958340449300524514">pull request accepted</a> into Rails which adds support for<a href="/misc/goto?guid=4958340450126244898">PostgreSQL's MACADDR, INET, and CIDR datatypes</a>. In Rails 4.0, the following migration will be supported:</p> <div class="highlight"> <pre><code class="ruby"><span class="n">create_table</span> <span class="ss">:network_types</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span> <span class="n"> t</span><span class="o">.</span><span class="n">cidr</span> <span class="ss">:cidr_address</span> <span class="n"> t</span><span class="o">.</span><span class="n">inet</span> <span class="ss">:ip_address</span> <span class="n"> t</span><span class="o">.</span><span class="n">macaddr</span> <span class="ss">:mac_address</span> <span class="k">end</span> </code></pre> </div> <p>Also, the schema dumper supports these types as well (previously they would appear as <code>string</code> types in the schema.rb file).</p> <p>ActiveRecord will also cast the values of the INET and CIDR types to Ruby's <a href="/misc/goto?guid=4958340450928475675">IPAddr</a>, while MACADDR will continue to be converted to a string.</p> </article> <p>via <a href="/misc/goto?guid=4958340451725722529" target="_blank">dockyard</a></p>