  
<div id="snippet_380" class="snippet">
  <h2><a href="/snippets/380-Validating-URLs-in-Rails" title="Validating URLs in Rails - Ruby - rails, validation, active_record, url">Validating URLs in Rails</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/124-Ruby">
      Ruby</a> posted 5 months ago by christian
          
  </div>

  <div class="body">
    <p><pre class="active4d"><span class="line-numbers">   1 </span> validates_format_of <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>website</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>with</span> =&gt; <span class="Variable">URI</span>::<span class="FunctionName">regexp</span>(<span class="String"><span class="String">%w(</span>http https<span class="String">)</span></span>)
</pre></p>
<p>via <a href="http://mbleigh.com/2009/02/18/quick-tip-rails-url-validation.html">http://mbleigh.com/2009/02/18/quick-tip-rails-url-validation.html</a></p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/9-rails">rails</a>, <a href="/tags/497-validation">validation</a>, <a href="/tags/606-active-record">active_record</a>, <a href="/tags/171-url">url</a>
    
    
  </div>
</div>



  
<div id="snippet_303" class="snippet">
  <h2><a href="/snippets/303-Twitter-type-followers-following-database-schema-for-Rails-ActiveRecord" title="Twitter type &quot;followers, following database schema&quot; for Rails/ActiveRecord - Ruby - twitter, follower, following, schema, active_record">Twitter type "followers, following database schema" for Rails/ActiveRecord</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/124-Ruby">
      Ruby</a> posted about 1 year ago by christian
          
  </div>

  <div class="body">
    <p>Draft schema for Twitter type followers, following functionality:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> create_table <span class="String"><span class="String">&quot;</span>followers<span class="String">&quot;</span></span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>force</span> =&gt; <span class="BuiltInConstant">true</span> <span class="Keyword">do </span>|<span class="Variable">t</span>|
<span class="line-numbers">   2 </span>     t.<span class="FunctionName">integer</span> <span class="String"><span class="String">&quot;</span>leader_id<span class="String">&quot;</span></span>
<span class="line-numbers">   3 </span>     t.<span class="FunctionName">integer</span> <span class="String"><span class="String">&quot;</span>follower_id<span class="String">&quot;</span></span>
<span class="line-numbers">   4 </span> <span class="Keyword">end</span>
<span class="line-numbers">   5 </span> 
<span class="line-numbers">   6 </span> add_index <span class="String"><span class="String">&quot;</span>followers<span class="String">&quot;</span></span>, [<span class="String"><span class="String">&quot;</span>leader_id<span class="String">&quot;</span></span>, <span class="String"><span class="String">&quot;</span>follower_id<span class="String">&quot;</span></span>], <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span> =&gt; <span class="String"><span class="String">&quot;</span>index_followers_on_leader_id_and_follower_id<span class="String">&quot;</span></span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>unique</span> =&gt; <span class="BuiltInConstant">true</span>
</pre></p>
<p>Just an idea, haven&#8217;t tested this yet.</p>
<p>Update: railscasts.com has published an article on how to use self-referential associations for this purpose:<br />
http://railscasts.com/episodes/163-self-referential-association</p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/665-twitter">twitter</a>, <a href="/tags/666-follower">follower</a>, <a href="/tags/667-following">following</a>, <a href="/tags/656-schema">schema</a>, <a href="/tags/606-active-record">active_record</a>
    
    
  </div>
</div>



  
<div id="snippet_270" class="snippet">
  <h2><a href="/snippets/270-How-to-update-the-ActiveRecord-counter-cache-magic-column" title="How to update the ActiveRecord counter_cache magic column - SQL - update, counter_cache, sql, rails, active_record">How to update the ActiveRecord counter_cache magic column</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/134-SQL">
      SQL</a> posted about 1 year ago by christian
          
  </div>

  <div class="body">
    <p>You can use the model.update_counters method to update the counter_cache column. But if you have a million rows it be very fast.</p>
<p>So for large tables it&#8217;s best to do it with a query such as this:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> <span class="Keyword">update</span> categories, (<span class="Keyword">select</span> 
<span class="line-numbers">   2 </span>                       id as category_id, ifnull(count, <span class="Number">0</span>) as count
<span class="line-numbers">   3 </span>                     <span class="Keyword">from</span> categories <span class="Keyword">left join</span> 
<span class="line-numbers">   4 </span>                       (<span class="Keyword">select</span> category_id, count(id) as count <span class="Keyword">from</span> products <span class="Keyword">group by</span> category_id) as count_table 
<span class="line-numbers">   5 </span>                     on 
<span class="line-numbers">   6 </span>                       categories.id = count_table.category_id) as count_table
<span class="line-numbers">   7 </span> <span class="Keyword">set</span> 
<span class="line-numbers">   8 </span>   categories.products_count = count_table.count
<span class="line-numbers">   9 </span> <span class="Keyword">where</span>
<span class="line-numbers">  10 </span>   categories.id = count_table.category_id
</pre></p>
<p>This query updates the count for all rows.</p>
<p>The code needs to be modified for your database design.</p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/604-update">update</a>, <a href="/tags/605-counter-cache">counter_cache</a>, <a href="/tags/255-sql">sql</a>, <a href="/tags/9-rails">rails</a>, <a href="/tags/606-active-record">active_record</a>
    
    
  </div>
</div>






