<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8521574239172426259</id><updated>2011-04-21T18:26:34.932-07:00</updated><category term='partials'/><category term='ruby'/><category term='generators'/><category term='add_index'/><category term='MySQL'/><category term='rails'/><category term='migrations'/><category term='views'/><category term='scaffolding'/><category term='DRY'/><category term='RoR'/><title type='text'>Nothing special</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://kirill-novitchenko.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8521574239172426259/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://kirill-novitchenko.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>KiNo</name><uri>http://www.blogger.com/profile/02785689777983382238</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://3.bp.blogspot.com/_Yo_ZNZfDOxU/SYniV2NYWVI/AAAAAAAAAC0/i-xixbPIUf8/S220/m.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8521574239172426259.post-403790814855243630</id><published>2009-02-18T09:21:00.000-08:00</published><updated>2009-02-18T09:49:09.659-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='generators'/><category scheme='http://www.blogger.com/atom/ns#' term='RoR'/><category scheme='http://www.blogger.com/atom/ns#' term='scaffolding'/><category scheme='http://www.blogger.com/atom/ns#' term='rails'/><category scheme='http://www.blogger.com/atom/ns#' term='views'/><category scheme='http://www.blogger.com/atom/ns#' term='partials'/><category scheme='http://www.blogger.com/atom/ns#' term='DRY'/><title type='text'>RoR scaffolding breaks DRY principle</title><content type='html'>Nothing special, but I just realized that Ruby on Rails scaffolding breaks one of RoR's main principles - &amp;quot;Don't Repeat Yourself&amp;quot; (or DRY).  When creating new and edit views for a new model, the exact same form is shown in both views:&lt;br /&gt;&lt;pre name="code" class="ruby"&gt;&lt;br /&gt;&amp;gt;script/generate scaffold articles title:string body:text&lt;br /&gt;...&lt;br /&gt;      create  app/views/articles/index.html.erb&lt;br /&gt;      create  app/views/articles/show.html.erb&lt;br /&gt;      create  app/views/articles/new.html.erb&lt;br /&gt;      create  app/views/articles/edit.html.erb&lt;br /&gt;      create  app/views/layouts/articles.html.erb&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If we look at new.html.erb we see&lt;br /&gt;&lt;pre name="code" class="ruby"&gt;&lt;br /&gt;&amp;lt;% form_for(@articles) do |f| %&amp;gt;&lt;br /&gt;  &amp;lt;p&amp;gt;&lt;br /&gt;    &amp;lt;b&amp;gt;Title&amp;lt;/b&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;    &amp;lt;%= f.text_field :title %&amp;gt;&lt;br /&gt;  &amp;lt;/p&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;p&amp;gt;&lt;br /&gt;    &amp;lt;b&amp;gt;Body&amp;lt;/b&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;    &amp;lt;%= f.text_area :body %&amp;gt;&lt;br /&gt;  &amp;lt;/p&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;p&amp;gt;&lt;br /&gt;    &amp;lt;%= f.submit &amp;quot;Create&amp;quot; %&amp;gt;&lt;br /&gt;  &amp;lt;/p&amp;gt;&lt;br /&gt;&amp;lt;% end %&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And looking at edit.html.erb we see&lt;br /&gt;&lt;pre name="code" class="ruby"&gt;&lt;br /&gt;&amp;lt;% form_for(@articles) do |f| %&amp;gt;&lt;br /&gt;  &amp;lt;p&amp;gt;&lt;br /&gt;    &amp;lt;b&amp;gt;Title&amp;lt;/b&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;    &amp;lt;%= f.text_field :title %&amp;gt;&lt;br /&gt;  &amp;lt;/p&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;p&amp;gt;&lt;br /&gt;    &amp;lt;b&amp;gt;Body&amp;lt;/b&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;    &amp;lt;%= f.text_area :body %&amp;gt;&lt;br /&gt;  &amp;lt;/p&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;p&amp;gt;&lt;br /&gt;    &amp;lt;%= f.submit &amp;quot;Update&amp;quot; %&amp;gt;&lt;br /&gt;  &amp;lt;/p&amp;gt;&lt;br /&gt;&amp;lt;% end %&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Looks like we are repeating ourselves here. If an object has many properties the amount of repeated code increases greatly.&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;The solution to this is using a partial for the fields and I think scaffold generator should be smart enough to do something like this:&lt;br /&gt;&lt;pre name="code" class="ruby"&gt;&lt;br /&gt;&amp;gt;script/generate scaffold articles title:string body:text&lt;br /&gt;...&lt;br /&gt;      create  app/views/articles/_form_fields.html.erb&lt;br /&gt;      create  app/views/articles/index.html.erb&lt;br /&gt;      create  app/views/articles/show.html.erb&lt;br /&gt;      create  app/views/articles/new.html.erb&lt;br /&gt;      create  app/views/articles/edit.html.erb&lt;br /&gt;      create  app/views/layouts/articles.html.erb&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Making new.html.erb look like&lt;br /&gt;&lt;pre name="code" class="ruby"&gt;&lt;br /&gt;&amp;lt;% form_for(@articles) do |f| %&amp;gt;&lt;br /&gt;  &amp;lt;% render :partial =&amp;gt; 'form_fields', :locals =&amp;gt; { :f =&amp;gt; f } %&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;p&amp;gt;&lt;br /&gt;    &amp;lt;%= f.submit &amp;quot;Create&amp;quot; %&amp;gt;&lt;br /&gt;  &amp;lt;/p&amp;gt;&lt;br /&gt;&amp;lt;% end %&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8521574239172426259-403790814855243630?l=kirill-novitchenko.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirill-novitchenko.blogspot.com/feeds/403790814855243630/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-novitchenko.blogspot.com/2009/02/ror-scaffolding-breaks-dry-principle.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8521574239172426259/posts/default/403790814855243630'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8521574239172426259/posts/default/403790814855243630'/><link rel='alternate' type='text/html' href='http://kirill-novitchenko.blogspot.com/2009/02/ror-scaffolding-breaks-dry-principle.html' title='RoR scaffolding breaks DRY principle'/><author><name>KiNo</name><uri>http://www.blogger.com/profile/02785689777983382238</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://3.bp.blogspot.com/_Yo_ZNZfDOxU/SYniV2NYWVI/AAAAAAAAAC0/i-xixbPIUf8/S220/m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8521574239172426259.post-5220306756911792729</id><published>2009-02-09T08:53:00.000-08:00</published><updated>2009-02-09T10:45:07.098-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='MySQL'/><category scheme='http://www.blogger.com/atom/ns#' term='RoR'/><category scheme='http://www.blogger.com/atom/ns#' term='migrations'/><category scheme='http://www.blogger.com/atom/ns#' term='add_index'/><title type='text'>Multi-column indexes in Ruby on Rails with MySQL</title><content type='html'>&lt;span style="font-weight:bold;"&gt;Nothing special&lt;/span&gt;, but I wanted to share two things that I learned yesterday while adding a multi-column index for one of my Ruby on Rails projects:&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;The book I use didn't show an example of what the declaration of such index could be in a migrations file.  So in case you were looking for such an example, here it is:&lt;br /&gt;&lt;pre name="code" class="ruby"&gt;&lt;br /&gt;add_index   :table_name,  [:column1, :column2, :column3], :unique =&amp;gt; true, :name =&amp;gt; 'some_name_here'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, just to clarify, add_index takes 3 parameters: name of the table you are adding index to, an array of column names to include in the index (could be just 1), and a hash of options that could specify whether the index is unique and assign a specific name to it (only these two options are supported by default).&lt;/li&gt;&lt;br /&gt;&lt;li&gt;When creating an index using a number of columns with long names I ran across an issue with limits on index name in MySQL. Here is the error message:&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;Mysql::Error: #33000Identifier name 'xxxx_xxx_xxx_xxx' is too long&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;Where xxx were names of columns. When creating an index, MySQL joins together the names of the columns to create index name; however, there is a limit of &lt;span style="font-weight:bold;"&gt;100 characters&lt;/span&gt; so if all names together in one string are longer you will get that error when trying to create an index.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;The solution is simple. Just use the &lt;span style="font-weight:bold;"&gt;name =&amp;gt; 'some_custom_short_name'&lt;/span&gt; as an option to add_index function call and supply your own short name for the index.&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;Hope this helps someone.&lt;br/&gt;&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;Here is this info in the context of a migration:&lt;br /&gt;&lt;pre name="code" class="ruby"&gt;&lt;br /&gt;class CreateTagLinks &amp;lt; ActiveRecord::Migration&lt;br /&gt;def self.up&lt;br /&gt; create_table :tag_links do |t|&lt;br /&gt;   t.integer :tag_id&lt;br /&gt;   t.integer :tagger_id&lt;br /&gt;   t.integer :url_id&lt;br /&gt;   t.boolean :is_accepted&lt;br /&gt;   t.timestamps&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; add_index :tag_links, [:tag_id, :tagger_id, :url_id], :unique =&gt; true&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.down&lt;br /&gt; drop_table :tag_links&lt;br /&gt;end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8521574239172426259-5220306756911792729?l=kirill-novitchenko.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirill-novitchenko.blogspot.com/feeds/5220306756911792729/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-novitchenko.blogspot.com/2009/02/multi-column-indexes-in-ruby-on-rails.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8521574239172426259/posts/default/5220306756911792729'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8521574239172426259/posts/default/5220306756911792729'/><link rel='alternate' type='text/html' href='http://kirill-novitchenko.blogspot.com/2009/02/multi-column-indexes-in-ruby-on-rails.html' title='Multi-column indexes in Ruby on Rails with MySQL'/><author><name>KiNo</name><uri>http://www.blogger.com/profile/02785689777983382238</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://3.bp.blogspot.com/_Yo_ZNZfDOxU/SYniV2NYWVI/AAAAAAAAAC0/i-xixbPIUf8/S220/m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8521574239172426259.post-2942266553391704186</id><published>2009-02-04T10:36:00.000-08:00</published><updated>2009-02-04T10:42:26.044-08:00</updated><title type='text'>Hello, World!</title><content type='html'>After all these years of reading blogs I finally decided to start my own.  Nothing special here - I'll just share some thoughts and findings from the work side of life.  I'm a geek at heart so I think a lot of content here will be fairly geeky; well, maybe someone will find it useful.  Later...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8521574239172426259-2942266553391704186?l=kirill-novitchenko.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirill-novitchenko.blogspot.com/feeds/2942266553391704186/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-novitchenko.blogspot.com/2009/02/hello-world.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8521574239172426259/posts/default/2942266553391704186'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8521574239172426259/posts/default/2942266553391704186'/><link rel='alternate' type='text/html' href='http://kirill-novitchenko.blogspot.com/2009/02/hello-world.html' title='Hello, World!'/><author><name>KiNo</name><uri>http://www.blogger.com/profile/02785689777983382238</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://3.bp.blogspot.com/_Yo_ZNZfDOxU/SYniV2NYWVI/AAAAAAAAAC0/i-xixbPIUf8/S220/m.jpg'/></author><thr:total>0</thr:total></entry></feed>
