<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description></description><title>On the chopping Bloc</title><generator>Tumblr (3.0; @choppingbloc)</generator><link>http://choppingbloc.tumblr.com/</link><item><title>Setting up MixPanel in Rails</title><description>&lt;p&gt;Mixpanel is simply awesome. It&amp;#8217;s simple to set up and has a great interface to track user generated events in real-time. Here&amp;#8217;s how I set it up in my Rails app.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Gem or no gem? &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As usually is the case, there is at least one &lt;a href="https://github.com/zevarito/mixpanel" target="_blank"&gt;Mixpanel gem&lt;/a&gt; out there. I opted NOT to use the gem because Mixpanel is constantly releasing new features (such as the People module) and the gem author doesn&amp;#8217;t seem to update it frequently enough. Plus, the Mixpanel is pretty simple to set up anyway.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Rails.env?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;First step is create TWO projects on Mixpanel: one for the development environment and for to track actual users (production). This is important in order to keep the real users events clean, but also have an environment where you can mess around in development.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Back on the Rails app, I created a view partial on &amp;#8216;shared/_mixpanel&amp;#8217;, where I inserted the standard JS snippet you can find one the Mixpanel instructions page. If you take look at that code, you will see the last line is where you should insert the project&amp;#8217;s token. Instead, I added a variable:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;  mixpanel.init(&amp;#8216;&amp;lt;%= MIXPANEL_TOKEN %&amp;gt;&amp;#8217;);&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;br/&gt;Then I wrapped the whole script around an if-statement that only runs if we are in development or production:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&amp;lt;% if Rails.env.staging? || Rails.env.production? %&amp;gt;&lt;br/&gt;&lt;/em&gt;&lt;em&gt;  (&amp;#8230;script&amp;#8230;)&lt;br/&gt;&lt;/em&gt;&lt;em&gt;&amp;lt;% end %&amp;gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;br/&gt;I put the actual tokens on their respective config/environment files (development.rb and production.rb):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;  MIXPANEL_TOKEN = &amp;#8220;1234somerandomnumber1234&amp;#8221;.freeze&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;br/&gt;Mixpanel should be ready to receive the events. Now all I had to do is include them in a JS asset file. For example:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;jQuery -&amp;gt;&lt;br/&gt;&lt;/em&gt;&lt;em&gt;  mixpanel.track_links(&amp;#8216;li #happy_button&amp;#8217;, &amp;#8216;Happy button clicked&amp;#8217;)&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;br/&gt;After clicking the button a few times, the events show up on the Mixpanel dashboard almost instantly.&lt;/p&gt;
&lt;p&gt;Most of these suggestions were taken from this &lt;a href="http://robots.thoughtbot.com/post/4026880618/javascript-integration-testing-example-installing-and" target="_blank"&gt;Thoughtbot post&lt;/a&gt;, so you might want to check it out as well.&lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/27369061426</link><guid>http://choppingbloc.tumblr.com/post/27369061426</guid><pubDate>Mon, 16 Jul 2012 20:25:22 -0400</pubDate><dc:creator>lucianot</dc:creator></item><item><title>Going deeper - Part III</title><description>&lt;p&gt;We successfully created the self-referential association for companies, going through two different models (see &lt;a href="http://choppingbloc.tumblr.com/post/25121628000/going-deeper-part-i" target="_blank"&gt;Part I &lt;/a&gt;and &lt;a href="http://choppingbloc.tumblr.com/post/25170948533/going-deeper-part-ii" target="_blank"&gt;Part II&lt;/a&gt;.) However, we still need a way to access a company&amp;#8217;s buyers directly from that object, so we need to do a few tweaks.&lt;/p&gt;
&lt;p&gt;The simplest way to tackle this problem is to create a method within Company that collects all the buyers from Dealing:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;def buyers&lt;br/&gt;  self.dealings.collect { |a| a.buyer }&lt;br/&gt;end  &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This approach has a few drawbacks. First, it may present performance issues since it makes one query for each buyer. Second, it is not very flexible, since you can only call &lt;em&gt;a_company.buyers&lt;/em&gt;, but not &lt;em&gt;a_company.investors&lt;/em&gt; or &lt;em&gt;a_deal.buyers&lt;/em&gt;. You could create new methods for the different roles and copy them into all the models, but that doesn&amp;#8217;t seem very &lt;a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself" target="_blank"&gt;DRY&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Another approach would be to create new associations for each type of buyer.  So, in Dealing, we would add:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;class Dealing &amp;lt; ActiveRecord::Base&lt;br/&gt;  belongs_to :deal&lt;br/&gt;  belongs_to :buyer, :polymorphic =&amp;gt; true&lt;br/&gt;&lt;strong&gt;  belongs_to :investor, :class_name =&amp;gt; 'Investor',&lt;/strong&gt;&lt;br/&gt;&lt;strong&gt;     :foreign_key =&amp;gt; 'buyer_id'&lt;/strong&gt;&lt;br/&gt;&lt;strong&gt;  belongs_to :corporate, :class_name =&amp;gt; 'Company',&lt;/strong&gt;&lt;br/&gt;&lt;strong&gt;     :foreign_key =&amp;gt; 'buyer_id'&lt;/strong&gt;&lt;br/&gt;end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that I called the Company type &amp;#8220;corporate&amp;#8221;, to avoid conflict with the name &amp;#8220;company&amp;#8221; which we already use in the opposite direction (&lt;em&gt;a_buyer.companies&lt;/em&gt;). How do we further propagate these associations to Deal and finally Company?&lt;/p&gt;
&lt;p&gt;&lt;img height="150" src="http://i.imgur.com/Flq66.jpg" width="550"/&gt;&lt;img height="150" src="http://i.imgur.com/Nh2fM.jpg" width="550"/&gt;&lt;img height="150" src="http://i.imgur.com/6Nony.jpg" width="550"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Kick&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In the movie Inception, waking someone from a dream involved a &amp;#8220;kick&amp;#8221;, which could be the sensation of falling into water or a sudden jolt that would startle the sleeper. However, if the person was in a dream within a dream, synchronized kicks at all levels were necessary to jolt the sleeper all the way back into the real world.&lt;/p&gt;
&lt;p&gt;Likewise, the changes we made to the app only allow Dealing to access its &amp;#8220;investor&amp;#8221; or &amp;#8220;corporate&amp;#8221;. We need to &amp;#8220;kick&amp;#8221; these relationships further up into the other models.&lt;/p&gt;
&lt;p&gt;The next level up is Deal, where we add a &lt;em&gt;has_many through&lt;/em&gt; for each buyer type:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;class Deal &amp;lt; ActiveRecord::Base&lt;br/&gt;  belongs_to :company&lt;br/&gt;  has_many :offerings, :class_name =&amp;gt; 'Dealing',&lt;br/&gt;     :foreign_key =&amp;gt; 'deal_id'&lt;br/&gt;&lt;strong&gt;  has_many :investors, :through =&amp;gt; :offerings,&lt;/strong&gt;&lt;br/&gt;&lt;strong&gt;     :source =&amp;gt; :buyer, :source_type =&amp;gt; 'Investor'&lt;/strong&gt;&lt;br/&gt;&lt;strong&gt;  has_many :corporates, :through =&amp;gt; :offering,&lt;/strong&gt;&lt;br/&gt;&lt;strong&gt;     :source =&amp;gt; :buyer, :source_type =&amp;gt; 'Company'&lt;/strong&gt;&lt;br/&gt;end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that we need to include the &lt;em&gt;source&lt;/em&gt; and &lt;em&gt;source_type&lt;/em&gt; options to indicate the source association name and type, since Rails cannot infer them automatically. &lt;/p&gt;
&lt;p&gt;The last level is Company itself, where we again add a &lt;em&gt;has_many through&lt;/em&gt; for each type:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;class Company &amp;lt; ActiveRecord::Base&lt;br/&gt;  has_many :offers, :class_name =&amp;gt; "Deal",&lt;br/&gt;     :foreign_key =&amp;gt; 'company_id'&lt;br/&gt;  has_many :offerings, :through =&amp;gt; :offers&lt;br/&gt;&lt;strong&gt;  has_many :investors, :through =&amp;gt; :offerings,&lt;br/&gt;      :source =&amp;gt; :investor,&lt;br/&gt;      :conditions =&amp;gt; "dealings.buyer_type = 'Investor'"&lt;br/&gt;   has_many :corporates, :through =&amp;gt; :offerings,&lt;br/&gt;      :source =&amp;gt; :corporate,&lt;br/&gt;      :conditions =&amp;gt; "dealings.buyer_type = 'Company'"&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;  has_many :dealings, :as =&amp;gt; :buyer&lt;br/&gt;  has_many :deals, :through =&amp;gt; :dealings&lt;br/&gt;  has_many :companies, :through =&amp;gt; :deals&lt;br/&gt;end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At two levels deep, it gets a bit tricker because there are two inner joins in the query. Therefore, simply passing &amp;#8220;&lt;em&gt;:source_type =&amp;gt; &amp;#8216;Company&amp;#8217;&lt;/em&gt;&amp;#8221; doesn&amp;#8217;t work. We need to specify that it is a &amp;#8220;&lt;em&gt;dealings.buyer_type&lt;/em&gt;&amp;#8221; via the &lt;em&gt;condition&lt;/em&gt; option.&lt;/p&gt;
&lt;p&gt;That should do the trick! We can finally use &amp;#8220;&lt;em&gt;a_target.investors&amp;#8221;&lt;/em&gt; or &amp;#8220;&lt;em&gt;a_target.corporates&amp;#8221;&lt;/em&gt; to return the company&amp;#8217;s buyers. We can also access them through any of the intermediary models, such as &amp;#8220;&lt;em&gt;a_deal.investors&amp;#8221;&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;One more thing&amp;#8230;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To close it all, we can now refactor our previous &lt;em&gt;buyers&lt;/em&gt; method to simply:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;def buyers&lt;br/&gt;  self.investors + self.corporates&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;&amp;gt; a_target_company.buyers&lt;br/&gt;# return arrays of investors &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;Success at last!&lt;/p&gt;
&lt;p&gt;Hoped you enjoyed this post. If you have any other interesting approaches, feel free to share.&lt;br/&gt; &lt;/p&gt;
&lt;p&gt;THE END &lt;br/&gt;(or is it?)&lt;/p&gt;
&lt;p&gt;&lt;img height="200" src="http://i.imgur.com/ayQRb.jpg" width="400"/&gt;&lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/25188359260</link><guid>http://choppingbloc.tumblr.com/post/25188359260</guid><pubDate>Fri, 15 Jun 2012 19:23:00 -0400</pubDate><category>rails</category><category>polymorphic</category><category>models</category><category>bloc</category><category>has_many</category><dc:creator>lucianot</dc:creator></item><item><title>Going deeper - Part II</title><description>&lt;p&gt;In the previous &lt;a href="http://choppingbloc.tumblr.com/post/25121628000/going-deeper-part-i" target="_blank"&gt;post&lt;/a&gt;, we created a polymorphic association between deals and buyers. Because of the polymorphism, buyers can now be either Investors or Corporates (the proxy model we created to represent companies when they are acting as buyers.)&lt;/p&gt;
&lt;p&gt;But how do we get rid of the Corporate model and allow a Company invest into other Companies? How do we design a model that references itself?&lt;br/&gt; &lt;/p&gt;
&lt;p&gt;&lt;img height="300" src="http://i.imgur.com/DHboD.gif" width="400"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Drawing hands&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The trick to resolving self-reference is to give different names to each role that the model can assume. On Escher&amp;#8217;s drawing above, you could call one arm a Hand, when it is out of the paper holding the pen and making a drawing. But you could also name it Sleeve, when it is being drawn on the paper. Therefore, a Hand can draw a Sleeve, even though it is technically the same object.&lt;/p&gt;
&lt;p&gt;In Rails, you can do the same by creating two associations with different names and passing the class name and foreign key. Here is the example from the Rails Guides:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;class Employee &amp;lt; ActiveRecord::Base&lt;br/&gt;  has_many :subordinates, :class_name =&amp;gt; "Employee",&lt;br/&gt;     :foreign_key =&amp;gt; "manager_id"&lt;br/&gt;  belongs_to :manager, :class_name =&amp;gt; "Employee"&lt;br/&gt;end &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An Employee can be a manager, who has many subordinates, or (s)he can be a subordinate, who belongs to a manager. Although they carry different names, the underlying model is still &amp;#8220;Employee&amp;#8221; as determined by the&amp;#160;:&lt;em&gt;class_name&lt;/em&gt; option. Note that the &lt;em&gt;has_many&lt;/em&gt; association also passes the name of the foreign key to look for in &amp;#8220;Subordinate&amp;#8221;. The Employee schema must have an integer column named &amp;#8216;manager_id&amp;#8217; in order for the self-joining association to work.&lt;/p&gt;
&lt;p&gt;In the Dealbook app, we can apply the same concept. First, let&amp;#8217;s add both roles to the Company model by simply copy/pasting from our Corporate model:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;class Company &amp;lt; ActiveRecord::Base&lt;br/&gt;  has_many :deals&lt;br/&gt;  has_many :dealings, :through =&amp;gt;= :deals&lt;br/&gt;&lt;br/&gt;  has_many :dealings, :as =&amp;gt; :buyer&lt;br/&gt;  has_many :deals, :through =&amp;gt; :dealings&lt;br/&gt;  has_many :companies, :through =&amp;gt; :deals&lt;br/&gt;end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we simply have to change the names in the roles, to set them apart.&lt;/p&gt;
&lt;p&gt;&lt;img height="340" src="http://i.imgur.com/KTz95.jpg" width="550"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A dream within a dream&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;But wait! We are still two models deep!&lt;/p&gt;
&lt;p&gt;In the case where a company invests in another company, it is not doing so directly (like in the Employee example). Rather, the Company (buyer) will have a Dealing, which belongs to a Deal, which belongs to another Company (target). Therefore, we need to create different names for &lt;strong&gt;all&lt;/strong&gt; of the links until we reach the target company. &lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s one way to rename the associations from the target Company&amp;#8217;s perspective:&lt;br/&gt;&lt;br/&gt;Deal =&amp;gt; Offer&lt;br/&gt;Dealing =&amp;gt; Offering&lt;br/&gt;Company =&amp;gt; Buyer &lt;/p&gt;
&lt;p&gt;Therefore, a buyer still has many dealings, each belonging to a deal, which belongs to a company. But now, a company has many offers, each having many offerings, each having many buyers.&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;class Company &amp;lt; ActiveRecord::Base&lt;br/&gt;&lt;strong&gt;  has_many :offers, :class_name =&amp;gt; "Deal",&lt;/strong&gt;&lt;br/&gt;&lt;strong&gt;     :foreign_key =&amp;gt; 'company_id'&lt;/strong&gt;&lt;br/&gt;&lt;strong&gt;  has_many :offerings, :through =&amp;gt; :offers&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;  has_many :dealings, :as =&amp;gt; :buyer&lt;br/&gt;  has_many :deals, :through =&amp;gt; :dealings&lt;br/&gt;  has_many :companies, :through =&amp;gt; :deals&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;class Deal &amp;lt; ActiveRecord::Base&lt;br/&gt;  belongs_to :company&lt;br/&gt;&lt;strong&gt;  has_many :offerings, :class_name =&amp;gt; "Dealing",&lt;/strong&gt;&lt;br/&gt;&lt;strong&gt;     :foreign_key =&amp;gt; 'deal_id'&lt;/strong&gt;&lt;br/&gt;end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This successfully implements the self-referencial associations on Company! Just like we did with investors, we can now access the companies invested by another company with:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;&amp;gt; a_buyer.class == Company&lt;br/&gt;&amp;gt; a_buyer.companies&lt;br/&gt;# returns arrays of companies invested by a_buyer&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But, as with investors, the unidirectional natural of polymorphic association prevents us from accessing a given company&amp;#8217;s investors:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;&amp;gt; a_company.buyers&lt;br/&gt;&amp;gt; a_company.investors&lt;br/&gt;# both throw NoMethodError&lt;br/&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As mentioned in the previous post, there a few solutions for this which we will see in the third and last part. Stay tuned!&lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/25170948533</link><guid>http://choppingbloc.tumblr.com/post/25170948533</guid><pubDate>Fri, 15 Jun 2012 14:34:00 -0400</pubDate><category>rails</category><category>bloc</category><category>ploymorphic</category><category>models</category><category>has_many</category><dc:creator>lucianot</dc:creator></item><item><title>Going deeper - Part I</title><description>&lt;p&gt;It&amp;#8217;s funny how seemingly simple features can trigger huge changes to an app&amp;#8217;s data structure.&lt;/p&gt;
&lt;p&gt;Case in point: my Dealbook app currently has three main models to store information on deals - Company, Investor and Deal. The Investor model represents the entity making the investment (buyer). The Company model represents the entity receiving the investment (seller). Deal is a &lt;em&gt;through&lt;/em&gt; model linking Companies and Investors, but also containing deal information, such as close date.&lt;/p&gt;
&lt;p&gt;The associations look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;class Company &amp;lt; ActiveRecord::Base&lt;br/&gt;   has_many :deals&lt;br/&gt;   has_many :investors, :through =&amp;gt; :deals&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;class Investor &amp;lt; ActiveRecord::Base&lt;br/&gt;   has_many_and_belongs_to_many :deals&lt;br/&gt;   has_many :companies, :through =&amp;gt; :deals&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;class Deal &amp;lt; ActiveRecord::Base&lt;br/&gt;   belongs_to :company&lt;br/&gt;   has_many_and_belongs_to_many :investors&lt;br/&gt;end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that a given Deal only belongs to one company, but has many investors. That makes sense, because a typical deal usually involves several investors putting money into a single company.&lt;/p&gt;
&lt;p&gt;So far, so good. My app was running happily with these model associations, until I ran into the following scenario: &lt;strong&gt;what if one of the investors is actually another company?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;With the current models, I would have to keep two separate records for the same company (both as Company and as Investor). This is obviously a bad idea, since the company&amp;#8217;s data would be scattered around. Ideally, the Company object should be able to store investments into other companies directly.&lt;/p&gt;
&lt;p&gt;It seems inevitable that we will need companies to reference themselves. Since dealing with self-references can be mind-numbing, I will skip it for now and address other issues first. To avoid the self-reference, I will use a workaround by temporarily creating a Corporate model to represent a company when it is the buyer. The current Company model will represent only the target company for now. Later, we will try to merge these two models into one.&lt;br/&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Polymorphism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The first step to solving the puzzle is finding a way to allow Deal buyers to be both Investors or Corporates. This most likely involves creating a polymorphic association between deals and buyers:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;class Company &amp;lt; ActiveRecord::Base&lt;br/&gt;  has_many :deals&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;class Deal &amp;lt; ActiveRecord::Base&lt;br/&gt;  belongs_to :company&lt;br/&gt;  belongs_to :buyer, :polymorphic =&amp;gt; true&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;class Investor &amp;lt; ActiveRecord::Base&lt;br/&gt;  has_many :deals, :as =&amp;gt; :buyer&lt;br/&gt;  has_many :companies, :through =&amp;gt; :deals&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;class Corporate &amp;lt; ActiveRecord::Base&lt;br/&gt;  has_many :deals, :as =&amp;gt; :buyer&lt;br/&gt;  has_many :companies, :through =&amp;gt; :deals&lt;br/&gt;end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is one problem here, however. (Can you spot it?) The current association between Deals and Investors is not &lt;em&gt;has_many&lt;/em&gt;, but rather &lt;em&gt;has_and_belongs_to_many&lt;/em&gt; (HABTM), which does &lt;strong&gt;not&lt;/strong&gt; accept polymorphism. Changing it to a simple &lt;em&gt;has_many&lt;/em&gt; relationship is not an option, since an investor can have many deals and a deal can have many investors.&lt;br/&gt; &lt;/p&gt;
&lt;p&gt;&lt;img height="226" src="http://i.imgur.com/Hz0LS.jpg" width="400"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Two levels deep&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;One solution would be to change the HABTM association into a &lt;em&gt;has_many through&lt;/em&gt;, which does allow polymorphism. Since we already have a &amp;#8216;&lt;em&gt;has_many through deals&lt;/em&gt;&amp;#8217; association, we would now have two intermediary models between target companies (Company) and their buyers (Investors or Corporates). Fortunately, multi-level &lt;em&gt;through&lt;/em&gt; associations are supported as of &lt;a href="http://guides.rubyonrails.org/3_1_release_notes.html" target="_blank"&gt;Rails 3.1&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here, I included a new &lt;em&gt;through&lt;/em&gt; model called Dealing:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;class Company &amp;lt; ActiveRecord::Base&lt;br/&gt;  has_many :deals&lt;br/&gt;  has_many :participations, :through =&amp;gt; :deals&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;class Deal &amp;lt; ActiveRecord::Base&lt;br/&gt;  belongs_to :company&lt;br/&gt;  has_many :dealings&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;class Dealing &amp;lt; ActiveRecord::Base&lt;br/&gt;  belongs_to :deal&lt;br/&gt;  belongs_to :buyer, :polymorphic =&amp;gt; true&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;class Investor &amp;lt; ActiveRecord::Base&lt;br/&gt;  has_many :dealings, :as =&amp;gt; :buyer&lt;br/&gt;  has_many :deals, :through =&amp;gt; :dealings&lt;br/&gt;  has_many :companies, :through =&amp;gt; :deals&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;class Corporate &amp;lt; ActiveRecord::Base&lt;br/&gt;  has_many :dealings, :as =&amp;gt; :buyer&lt;br/&gt;  has_many :deals, :through =&amp;gt; :dealings&lt;br/&gt;  has_many :companies, :through =&amp;gt; :deals&lt;br/&gt;end &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works! Now we can access companies directly from an investor by simply using:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;&amp;gt; an_investor.companies&lt;br/&gt;# returns array of companies invested by an_investor &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The only catch is that polymorphic &lt;em&gt;through&lt;/em&gt; associations only go in one direction, so trying the opposite call does not work:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;&amp;gt; a_company.buyers&lt;br/&gt;&amp;gt; a_company.investors&lt;br/&gt;&amp;gt; a_company.corporates&lt;br/&gt;# all of the above throw NoMethodError&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are a few ways around this, which I will cover later. (If you&amp;#8217;re curious, check out &lt;a href="http://blog.hasmanythrough.com/2006/4/3/polymorphic-through" target="_blank"&gt;this post&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;So now that we can accept both types of buyers in Deals, it&amp;#8217;s time to eliminate the Corporate model and create a self-referencial Company.&lt;/p&gt;
&lt;p&gt; &lt;br/&gt;TO BE CONTINUED&amp;#8230;&lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/25121628000</link><guid>http://choppingbloc.tumblr.com/post/25121628000</guid><pubDate>Thu, 14 Jun 2012 19:25:00 -0400</pubDate><category>rails</category><category>bloc</category><category>models</category><category>polymorphic</category><category>has_many</category><dc:creator>lucianot</dc:creator></item><item><title>Multiple select boxes with Chosen jQuery</title><description>&lt;p&gt;After long hours of sweat and frustration, I finally got the multiple select box to work with the Chosen plugin. It seems other people are having some trouble as well, so I will lay out the steps I followed:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Get the form working&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;First, I recommend getting the multiple select box to work &lt;strong&gt;without&lt;/strong&gt; Chosen first. It should be fully functional, but without the cool JS effect that Chosen will provide. For reference, it should look like &lt;a href="http://harvesthq.github.com/chosen/" target="_blank"&gt;this&lt;/a&gt; (second row, left side).&lt;/p&gt;
&lt;p&gt;In my app I have two models, Company and Market, which are related to one another through a has_and_belongs_to_many association. In the new Company form, I want to be able to pick one or more markets for a given company. (I will assume here that all the models, migrations and join table have been properly set up.)&lt;/p&gt;
&lt;p&gt;If you are using a gem like &amp;#8216;&lt;a href="https://github.com/plataformatec/simple_form" target="_blank"&gt;simple_form&lt;/a&gt;&amp;#8217;, creating the form should be a piece of cake. With &amp;#8216;simple_form&amp;#8217;, all it takes is one line inside your company form:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&amp;lt;%= f.association :markets %&amp;gt; &lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Just pass in the model name (plural if has_many). The gem will locate the HABTM association and create a multiple select form.&lt;/p&gt;
&lt;p&gt;If you are not using a gem, it&amp;#8217;s a bit tricker, but should work as well:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&amp;lt;div class=&amp;#8221;field&amp;#8221;&amp;gt;&lt;/em&gt;&lt;br/&gt;&lt;em&gt;  &amp;lt;%= f.label :market_ids, &amp;#8216;Markets&amp;#8217; %&amp;gt;&amp;lt;br /&amp;gt;&lt;/em&gt;&lt;br/&gt;&lt;em&gt;  &amp;lt;%= f.collection_select :market_ids, Market.order(:name), :id, :name, {}, {multiple: true} %&amp;gt;&lt;/em&gt;&lt;br/&gt;&lt;em&gt;&amp;lt;/div&amp;gt; &lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Notice the &lt;em&gt;&amp;#8216;multiple: true&amp;#8217;&lt;/em&gt; option that needs to be included to make the select box accept multiple items.&lt;/p&gt;
&lt;p&gt;Whether you used a gem or not, the form should now be working. Make sure you test it by creating a few new records before moving to the next step.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Add Chosen&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For simple integration, I used the &amp;#8216;&lt;a href="https://github.com/tsechingho/chosen-rails" target="_blank"&gt;chosen-rails&lt;/a&gt;&amp;#8217; gem. After you add it to your Gemfile and run bundle, you need to add a few lines of code.&lt;/p&gt;
&lt;p&gt;In &lt;em&gt;&amp;#8216;app/assets/javascripts/application.js&amp;#8217;&lt;/em&gt;, add:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;//= require chosen-jquery&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In &lt;em&gt;&amp;#8216;app/assets/stylesheets/application.css&amp;#8217;&lt;/em&gt;, add:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;*= require chosen&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Once the gem is installed, we need to add the JS (or rather CoffeeScript) code. Inside the &lt;em&gt;&amp;#8216;app/assets/javascripts&amp;#8217;&lt;/em&gt; folder, I created a &lt;em&gt;&amp;#8216;companies.js.coffee&amp;#8217;&lt;/em&gt; file with:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;jQuery -&amp;gt;&lt;br/&gt;&lt;/em&gt;&lt;em&gt;  $(&amp;#8216;#company_market_ids&amp;#8217;).chosen()&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Notice that the selector must have this name, since it is the default id created by the form. It includes the name of the current model (company) and the object ids of the select box options (market_ids). If you want to use a different selector, you will need to change the default name that is generated by the form.&lt;/p&gt;
&lt;p&gt;Bingo! The select box should be working properly with the Chosen plugin magic, just like &lt;a href="http://harvesthq.github.com/chosen/" target="_blank"&gt;this&lt;/a&gt; (second row, right side).&lt;/p&gt;
&lt;p&gt;(If you get a &amp;#8220;&lt;span&gt;couldn&amp;#8217;t find file &amp;#8216;chosen&amp;#8217;&lt;/span&gt;&amp;#8221; error message, try restarting the rails server.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Extra configs&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are few handy configurations you can do. &lt;/p&gt;
&lt;p&gt;If you want to change the default style, simple add a &lt;em&gt;&amp;#8216;app/assets/stylesheets/companies.css.scss&amp;#8217;&lt;/em&gt; file and use the same CSS id selector. For example, we can increase the field width with:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;#company_market_ids { width: 400px; }&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you wish to change the placeholder (text inside the select box), you can include this option in your field generator (form view file):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&amp;lt;%= f.association :markets, :input_html =&amp;gt; {:class =&amp;gt; &amp;#8220;chzn-select&amp;#8221;, &amp;#8221;data-placeholder&amp;#8221; =&amp;gt; &amp;#8220;Select one or more markets&amp;#8221;} %&amp;gt; &lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;For more options, check out the Chosen and simple_form websites.&lt;/p&gt;
&lt;p&gt;I hope this was helpful. If there are other interesting ways to use Chosen, feel free to add comments.&lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/24894460392</link><guid>http://choppingbloc.tumblr.com/post/24894460392</guid><pubDate>Mon, 11 Jun 2012 14:05:00 -0400</pubDate><dc:creator>lucianot</dc:creator></item><item><title>Managing project with Trello</title><description>&lt;p&gt;&lt;a href="https://trello.com/" target="_blank"&gt;Trello&lt;/a&gt; is an awesome project management board developed by &lt;a href="http://joelonsoftware.com/" target="_blank"&gt;Joel Spolsky&lt;/a&gt;, of &lt;a href="http://stackoverflow.com/" target="_blank"&gt;Stack Overflow&lt;/a&gt; fame. I have been using it to keep track of all the project features and I highly recommmend it.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve made my project board public, so I invite everyone to take a look at it. Feel free to post comments or upvote features directly on Trello:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://trello.com/board/brazil-dealbook/4fae902a926b6b22642012a3" target="_blank"&gt;Luciano&amp;#8217;s Dealbook Trello Board&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;BONUS: the Trello team has made Trello&amp;#8217;s own project board public as well, so you can actually use Trello to monitor and influence Trello&amp;#8217;s development.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Yo Dawg" height="180" src="http://i.imgur.com/CHlGQ.jpg" width="280"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://trello.com/board/trello-development/4d5ea62fd76aa1136000000c" target="_blank"&gt;Trello Trello Board&lt;/a&gt;&lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/24746861808</link><guid>http://choppingbloc.tumblr.com/post/24746861808</guid><pubDate>Sat, 09 Jun 2012 10:57:00 -0400</pubDate><dc:creator>lucianot</dc:creator></item><item><title>Ruby symbols</title><description>&lt;p&gt;One of the first things you notice when you glance at Ruby code are those curious words prefixed with colons. In fact, these interesting constructs are very peculiar to Ruby and are called symbols.&lt;/p&gt;
&lt;p&gt;But what are Ruby symbols? They are simply a name or identifier for other objects. They are not strings or numbers, but rather an efficient way to represent them.&lt;/p&gt;
&lt;p&gt;One important characteristic of symbols is they are immutable and, therefore, you can&amp;#8217;t assign it a value. You will never see the following code:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;gt; :some_symbol = &amp;#8220;some string&amp;#8221;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The other important feature is that, unlike Strings, Symbols that have the same name are actually the same object. For example:&lt;/p&gt;
&lt;p&gt;&amp;gt; &amp;#8220;some_string&amp;#8221;.object_id == &amp;#8220;some_string&amp;#8221;.object_id&lt;br/&gt;&lt;em&gt;=&amp;gt; false&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;gt; :some_symbol.object_id == :some_symbol.object_id&lt;br/&gt;=&amp;gt; true&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;As we can see, Ruby creates two different String objects, even though the contents are the same. In the Symbol case, there is only a single object.&lt;/p&gt;
&lt;p&gt;Why are symbols so useful? Aside from making writing code easier, symbols are very memory efficient. Because of their immutability, they are perfect to represent an object you will use many times.&lt;/p&gt;
&lt;p&gt;A common example is hash keys. You could simply use strings for hash keys but, as we saw above, this would create a new object for every record even if the keys have the same name. Given enough records, you would eventually run out of memory. Using a symbol for the key, we avoid this problem since all of the like keys would point to the same object.&lt;/p&gt;
&lt;p&gt;A similar situation occurs in Rails, where we use hundreds of links and methods over and over. Rails makes good use of symbols by choosing them not only for hash keys, but also to represent different objects like method and attribute names.&lt;/p&gt;
&lt;p&gt;If you create a custom validation method, you would pass its symbol representation:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;validate :some_custom_validation, :on =&amp;gt; :create&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Note that the option also uses symbols to represent both the option key and the &lt;em&gt;create&lt;/em&gt; controller method.&lt;/p&gt;
&lt;p&gt;In summary, symbols are a simple but powerful construct that allows Ruby and Rails to run faster and more efficiently.&lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/24373374235</link><guid>http://choppingbloc.tumblr.com/post/24373374235</guid><pubDate>Sun, 03 Jun 2012 21:21:00 -0400</pubDate><dc:creator>lucianot</dc:creator></item><item><title>Proc vs lambda</title><description>&lt;p&gt;Lambdas and Procs are a fun and powerful part of Ruby. In essence, they are just a chunk of code that can be stored in a variable  and called later. For example:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;gt; double = lambda { |n| n*2 } &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The code inside the brackets will take whatever number is passed to it and multiply it by two. However, the code is not executed during the assigment. It is simply stored in a the variable &lt;em&gt;double&lt;/em&gt; to be executed later.&lt;/p&gt;
&lt;p&gt;To do that, we simply &lt;em&gt;call&lt;/em&gt; the object:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;gt; double.call 2&lt;/em&gt;&lt;br/&gt;&lt;em&gt;=&amp;gt; 4 &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Here we are calling the &amp;#8220;chunk of code&amp;#8221; called &lt;em&gt;double&lt;/em&gt; and passing it the number 2. It will take this number and execute the code (double it).&lt;/p&gt;
&lt;p&gt;A Proc object works the same way:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;gt; triple = Proc.new { |n| n*3 }&lt;br/&gt;&amp;gt; triple.call 3&lt;br/&gt;=&amp;gt; 9&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In fact, lambdais just a Kernel method that creates a Proc object!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;gt; what_is_lambda = lambda { |n| puts &amp;#8220;Surprise&amp;#8221; }&lt;br/&gt;&lt;/em&gt;&lt;em&gt;&amp;gt; what_is_lambda.class&lt;br/&gt;&lt;/em&gt;&lt;em&gt;=&amp;gt; Proc &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Diff&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are, however, slight differences between using lambda and Proc.new. First, lambda checks the number of arguments that are passed to it, while Proc doesn&amp;#8217;t. Let&amp;#8217;s see an example:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;gt; some&lt;/em&gt;&lt;em&gt;_lambda&lt;/em&gt;&lt;em&gt; = lambda { |x, y| puts &amp;#8220;#{x} and #{y}&amp;#8221; }&lt;/em&gt;&lt;br/&gt;&lt;em&gt;&amp;gt; some_proc = Proc.new  { |x, y| puts &amp;#8220;#{x} and #{y}&amp;#8221; }&lt;/em&gt;&lt;br/&gt;&lt;br/&gt;Both contain the same code, however if we pass only one argument:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;some&lt;/em&gt;&lt;em&gt;_lambda&lt;/em&gt;&lt;em&gt;.call(&amp;#8220;cats&amp;#8221;)&lt;/em&gt;&lt;br/&gt;&lt;em&gt;ArgumentError: wrong number of arguments (1 for 2)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;some_proc&lt;/em&gt;&lt;em&gt;.call(&amp;#8220;cats&amp;#8221;)&lt;/em&gt;&lt;br/&gt;&lt;em&gt;=&amp;gt;  &amp;#8221;cats and&amp;#8221;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Proc executes normally, ignoring the missing argument, while lambda throws an Error.&lt;/p&gt;
&lt;p&gt;The second main difference between the two is how they deal with &lt;em&gt;return&lt;/em&gt; statements. If a &lt;em&gt;return&lt;/em&gt; statement is included in a Proc, it will be treated like a method &lt;em&gt;return&lt;/em&gt; statement:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;def some_method&lt;/em&gt;&lt;br/&gt;&lt;em&gt;  some_proc = Proc.new { return &amp;#8220;Method ends here.&amp;#8221; }&lt;/em&gt;&lt;br/&gt;&lt;em&gt;  some_proc.call&lt;/em&gt;&lt;br/&gt;&lt;em&gt;  return &amp;#8220;This never happens.&amp;#8221;&lt;/em&gt;&lt;br/&gt;&lt;em&gt;end&lt;/em&gt;&lt;br/&gt;&lt;br/&gt;&lt;em&gt;&amp;gt; some_method&lt;/em&gt;&lt;br/&gt;&lt;em&gt;=&amp;gt; &amp;#8220;Method ends here.&amp;#8221; &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;However, when we use lambda, it simply exits from itself and the method continues executing:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;def some_method&lt;/em&gt;&lt;br/&gt;&lt;em&gt;  some_lambda= lambda { return &amp;#8220;Simply exits lambda.&amp;#8221; }&lt;br/&gt;&lt;/em&gt;&lt;em&gt;  some_lambda&lt;/em&gt;&lt;em&gt;.call&lt;/em&gt;&lt;br/&gt;&lt;em&gt;  return &amp;#8220;This is returned.&amp;#8221;&lt;/em&gt;&lt;br/&gt;&lt;em&gt;end&lt;/em&gt;&lt;br/&gt;&lt;br/&gt;&lt;em&gt;&amp;gt; some_method&lt;/em&gt;&lt;br/&gt;&lt;em&gt;=&amp;gt; &amp;#8220;&lt;/em&gt;&lt;em&gt;This is returned&amp;#8221;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Blocks&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So, how are blocks related to Procs? A block is simply a Proc that hasn&amp;#8217;t been created yet. It is an anonymous &amp;#8220;chunk of code&amp;#8221; that needs to be bound to a method and converted into a Proc before being used. &lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;gt; { |n| puts &amp;#8220;Hello&amp;#8221; }&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This throws an Error. On its own, the block does nothing. It needs to passed to a method first:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;gt; 10.times { |n| puts &amp;#8220;Hello&amp;#8221; }&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Under the hood, &lt;em&gt;times&lt;/em&gt; will convert the block into a Proc and execute it when called within the method. Here is an interesting example to illustrate this:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;def what_is_block(&amp;amp;block)&lt;/em&gt;&lt;br/&gt;&lt;em&gt;  puts block.class&lt;/em&gt;&lt;br/&gt;&lt;em&gt;end &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;gt; what_is_block { |n| puts &amp;#8220;Surprise&amp;#8221; }&lt;/em&gt;&lt;br/&gt;&lt;em&gt;=&amp;gt; Proc &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Once the block is passed into a method, it becomes a Proc!&lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/24232277356</link><guid>http://choppingbloc.tumblr.com/post/24232277356</guid><pubDate>Fri, 01 Jun 2012 21:45:00 -0400</pubDate><dc:creator>lucianot</dc:creator></item><item><title>Length vs. presence validations</title><description>&lt;p&gt;Today I learned (the hard way) that ActiveRecord length validations when a range is used  implicitly validates presence as well. I previously assumed that length would only be validated if the attribute was present, and that presence needed to be validated separately using &lt;em&gt;:presence =&amp;gt; true&lt;/em&gt;. But that is not the case.&lt;/p&gt;
&lt;p&gt;For example, this code would not validate if name is nil.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;validates :name, :length =&amp;gt; { :in =&amp;gt; 2..100 }&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;To obtain a validation that checks length only if the attribute is present, I had to include the :allow_nil option:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt; validates :name, :length =&amp;gt; { :in =&amp;gt; 2..100, :allow_nil =&amp;gt; true }&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;EDIT: &lt;a href="https://gist.github.com/2829102" target="_blank"&gt;Proof&lt;/a&gt;&lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/23949091193</link><guid>http://choppingbloc.tumblr.com/post/23949091193</guid><pubDate>Mon, 28 May 2012 16:32:00 -0400</pubDate><dc:creator>lucianot</dc:creator></item><item><title>Test-driven development</title><description>&lt;p&gt;I always liked the TDD process. Write the tests first, write the minimum code to make the tests pass and then refactor. As a beginner, it helps me in a few ways:&lt;/p&gt;
&lt;ol&gt;&lt;li&gt;As I write the tests, I am forced to think about the exact results I want to acheive;&lt;/li&gt;
&lt;li&gt;By checking the error messages for failing tests, I know what to do next;&lt;/li&gt;
&lt;li&gt;Having a good test coverage gives me some comfort that the wheels are not going to fall off the app.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;This may be an overkill for more experienced developers, but it certainly helps me. For Dealbook, I&amp;#8217;ll write all model and integration tests using &lt;a href="https://github.com/rspec/rspec-rails" target="_blank"&gt;Rspec&lt;/a&gt; + &lt;a href="https://github.com/jnicklas/capybara" target="_blank"&gt;Capybara&lt;/a&gt;. For factories, I&amp;#8217;m using &lt;a href="https://github.com/notahat/machinist" target="_blank"&gt;Machinist&lt;/a&gt; + &lt;a href="http://faker.rubyforge.org/" target="_blank"&gt;Faker&lt;/a&gt;. (&lt;a href="https://github.com/thoughtbot/factory_girl" target="_blank"&gt;FactoryGirl&lt;/a&gt; is more popular, but Machinist seems to better handle has_and_belongs_to_many associations.)&lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/23948502322</link><guid>http://choppingbloc.tumblr.com/post/23948502322</guid><pubDate>Mon, 28 May 2012 16:22:00 -0400</pubDate><dc:creator>lucianot</dc:creator></item><item><title>Reading material</title><description>&lt;p&gt;For the first week, we mostly had to go over the reading material, which was tailored to each student&amp;#8217;s skill level in each area. The topic I felt the least comfortable with was Javascript, so I was assigned to read &amp;#8220;&lt;a href="http://www.amazon.com/JavaScript-jQuery-The-Missing-Manual/dp/1449399029/ref=sr_1_12?s=books&amp;amp;ie=UTF8&amp;amp;qid=1336548460&amp;amp;sr=1-12" target="_blank"&gt;JavaScript &amp;amp; JQuery: The Missing Manual&lt;/a&gt;&amp;#8221; by David McFarland. The book was a great to intro to JS/JQuery and contained some cool exercises for each chapter.&lt;/p&gt;
&lt;p&gt;I also felt I needed some help with using the Terminal and Git, so Jared recommended a few online tutorials. For review purposes, I also had to read &amp;#8220;&lt;a href="Beginning%20Ruby:%20From%20Novice%20to%20Professional" target="_blank"&gt;Beginning Ruby&lt;/a&gt;&amp;#8221; by Peter Cooper and the official &lt;a href="http://guides.rubyonrails.org/" target="_blank"&gt;Rails Guides&lt;/a&gt;. (All the paid books were included in the tuition fee.)&lt;/p&gt;
&lt;p&gt;It was a lot of reading for one week, but it felt good to get it out of the way and focus on the fun stuff.&lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/23643783028</link><guid>http://choppingbloc.tumblr.com/post/23643783028</guid><pubDate>Wed, 23 May 2012 21:10:00 -0400</pubDate><category>bloc</category><category>reading</category><dc:creator>lucianot</dc:creator></item><item><title>Crunchbase meets AngelList for Brazil</title><description>&lt;p&gt;The app I want to build during the course is a database for Brazilian startups, where users can find information about companies, investment firms, people and events. The app will be collaborative, so any user can signup and insert new records or edit the information. &lt;/p&gt;
&lt;p&gt;As I mentioned on the previous post, the current Google Spreadsheet (&lt;a href="http://bit.ly/GDLZgt" target="_blank"&gt;link&lt;/a&gt;) is reasonably active and is already the main reference for the local market. I was hoping to move this &amp;#8220;community&amp;#8221; to a more structured app and enhance some of the features which are not possible in the current format.&lt;/p&gt;
&lt;p&gt;For class, our first step was to create the wireframes for the project, in order to present it to our mentors. I used &lt;a href="http://www.balsamiq.com/" target="_self"&gt;Balsamiq&lt;/a&gt; which is very easy to use. Here is what I came up with:&lt;/p&gt;
&lt;p&gt;Homepage&lt;/p&gt;
&lt;p&gt;&lt;a href="http://i.imgur.com/lYq0Y.png"&gt;&lt;img align="middle" alt="Homepage" height="350" src="http://i.imgur.com/lYq0Y.png" width="500"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;New company form&lt;/p&gt;
&lt;p&gt;&lt;a href="http://i.imgur.com/VEsT0.png"&gt;&lt;img align="middle" alt="New company" height="350" src="http://i.imgur.com/VEsT0.png" width="500"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Company page&lt;/p&gt;
&lt;p&gt;&lt;a href="http://i.imgur.com/j9GQU.png"&gt;&lt;img align="middle" alt="Company page" height="350" src="http://i.imgur.com/j9GQU.png" width="500"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Person page&lt;/p&gt;
&lt;p&gt;&lt;a href="http://i.imgur.com/oUtmq.png"&gt;&lt;img align="middle" alt="Person page" height="350" src="http://i.imgur.com/oUtmq.png" width="500"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Companies index page&lt;/p&gt;
&lt;p&gt;&lt;a href="http://i.imgur.com/b0hZu.png"&gt;&lt;img align="middle" alt="Companies" height="350" src="http://i.imgur.com/b0hZu.png" width="500"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;On my last call with Jared, I showed him the wireframes and talked about the features I would like to include, such as revision tracking, multi-language support and advanced search. He thought the scope was adequate for a 3-4 week project and gave the green light.&lt;/p&gt;
&lt;p&gt;Game on!&lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/23587976734</link><guid>http://choppingbloc.tumblr.com/post/23587976734</guid><pubDate>Tue, 22 May 2012 22:51:00 -0400</pubDate><category>bloc</category><category>wireframes</category><dc:creator>lucianot</dc:creator></item><item><title>Hello World</title><description>&lt;p&gt;A year ago, I decided to learn how to code. So far, I&amp;#8217;ve read a bunch of programming books and did most of the online courses out there. Stanford, Codecademy, Udacity, you name it. While they gave me a good theoretical foundation, it always felt different when I actually had to create a real app on my own. I missed having someone more experienced to review my code and make sure I was doing things right.&lt;/p&gt;
&lt;p&gt;When I heard about Bloc, the idea of taking a 2-month intensive course with heavy mentoring immediately appealed to me. I also enjoyed the fact that the format was very flexible and the content would be customized to my own level of experience. More importantly, most of my time would be spent building real apps, not listening to lectures.&lt;/p&gt;
&lt;p&gt;Long story short: I signup up, interviewed, got accepted and class has already started officially this week. Each student is assigned a mentor, who will oversee their progress over the whole course. I got assigned to Jared, who is one of the founders of Bloc. &lt;/p&gt;
&lt;p&gt;My initial task was to choose an idea for an app to be developed individually over the next 3-4 weeks. The app I chose is a Crunchbase/AngelList for Brazilians startups. I already help run an active collaborative &lt;a href="http://bit.ly/GDLZgt" target="_blank"&gt;Google Spreadsheet&lt;/a&gt; that is used by local startups and investors, so the idea would be to move this data to a more structured web app with improved features. &lt;/p&gt;
&lt;p&gt;I will be talking a bit more about the project and the wireframes on the next post. &lt;/p&gt;</description><link>http://choppingbloc.tumblr.com/post/23346420766</link><guid>http://choppingbloc.tumblr.com/post/23346420766</guid><pubDate>Sat, 19 May 2012 09:05:00 -0400</pubDate><category>bloc</category><category>rails</category><dc:creator>lucianot</dc:creator></item></channel></rss>
