<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>class Nasir &#60; Jamal</title>
	<atom:link href="http://nasir.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://nasir.wordpress.com</link>
	<description>self.blog(social_network, ruby_on_rails, technology_options = {})</description>
	<pubDate>Fri, 18 Apr 2008 04:04:48 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
	<language>en</language>
			<item>
		<title>Formatting/styling will_paginate</title>
		<link>http://nasir.wordpress.com/2008/04/16/formattingstyling-will_paginate/</link>
		<comments>http://nasir.wordpress.com/2008/04/16/formattingstyling-will_paginate/#comments</comments>
		<pubDate>Wed, 16 Apr 2008 09:17:28 +0000</pubDate>
		<dc:creator>nasir</dc:creator>
		
		<category><![CDATA[Ruby On Rails]]></category>

		<category><![CDATA[formatting pagination links]]></category>

		<category><![CDATA[styling pagination]]></category>

		<category><![CDATA[will_paginate]]></category>

		<guid isPermaLink="false">http://nasir.wordpress.com/?p=43</guid>
		<description><![CDATA[
I received few requests in the past ( personally and recently a comment on my post describing how to use will_paginate plugin ) about how to format pagination links while using will_paginate plugin.
So here it is:
When you add
&#60;%= will_paginate @obj_instance %&#62;
in your views to show your pagination links then it also creates an element (div) [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><iframe src='http://digg.com/api/diggthis.php?u=http%3A%2F%2Fdigg.com%2Fprogramming%2FFormatting_Styling_pagination_links_will_paginate' height='82' width='55' frameborder='0' scrolling='no' style='float: right; margin-left: 10px; margin-bottom: 5px; padding: 4px 0 2px 4px; background: #fff;'></iframe><br />
I received few requests in the past ( personally and recently a comment on <a href="http://nasir.wordpress.com/2007/10/31/pagination-in-ruby-on-rails-using-will_paginate-plugin">my post describing how to use will_paginate plugin</a> ) about how to format pagination links while using will_paginate plugin.</p>
<p>So here it is:</p>
<p>When you add</p>
<p><code>&lt;%= will_paginate @obj_instance %&gt;</code></p>
<p>in your views to show your pagination links then it also creates an element (div) and wraps the whole thing under the class name pagination. The one with the current page will have the class name current.</p>
<p>Here is the html code generated by will_paginate</p>
<p><code><br />
&lt;div class=&#8221;pagination&#8221;&gt;<br />
&lt;a href=&#8221;/page_path?page=2&#8243;&gt;« Previous&lt;/a&gt;<br />
&lt;a href=&#8221;/page_path&#8221;&gt;1&lt;/a&gt;<br />
&lt;a href=&#8221;/page_path?page=2&#8243;&gt;2&lt;/a&gt;<br />
&lt;span class=&#8221;current&#8221;&gt;3&lt;/span&gt;<br />
&lt;a href=&#8221;/page_path?page=4&#8243;&gt;4&lt;/a&gt;<br />
&lt;a href=&#8221;/page_path?page=5&#8243;&gt;5&lt;/a&gt;<br />
&lt;a href=&#8221;/page_path?page=4&#8243;&gt;Next »&lt;/a&gt;<br />
&lt;/div&gt;<br />
</code></p>
<p>Now, it is totally upto your imagination how you want to style it using CSS.</p>
<p>But, if due to some reason you don’t want to use the class pagination and add your own then do something like this</p>
<p><code><br />
&lt;%= will_paginate @obj_instance, :class =&gt; &#8216;my_class&#8217; %&gt;<br />
</code></p>
<p>Similarly, if you want to add an element id then<br />
<code><br />
&lt;%= will_paginate @obj_instance, :id =&gt; &#8216;my_element&#8217; %&gt;<br />
</code></p>
<p>Let&#8217;s say if you have done both like so:<br />
<code><br />
&lt;%= will_paginate @obj_instance, :id =&gt; &#8216;my_element&#8217;, :class =&gt; &#8216;my_class&#8217; %&gt;<br />
</code></p>
<p>then the new html generated by will_paginate will be<br />
<code><br />
&lt;div class=&#8221;my_class&#8221; id=&#8221;my_element&#8221;&gt;<br />
&lt;a href=&#8221;/page_path?page=2&#8243;&gt;« Previous&lt;/a&gt;<br />
&lt;a href=&#8221;/page_path&#8221;&gt;1&lt;/a&gt;<br />
&lt;a href=&#8221;/page_path?page=2&#8243;&gt;2&lt;/a&gt;<br />
&lt;span class=&#8221;current&#8221;&gt;3&lt;/span&gt;<br />
&lt;a href=&#8221;/page_path?page=4&#8243;&gt;4&lt;/a&gt;<br />
&lt;a href=&#8221;/page_path?page=5&#8243;&gt;5&lt;/a&gt;<br />
&lt;a href=&#8221;/page_path?page=4&#8243;&gt;Next »&lt;/a&gt;<br />
&lt;/div&gt;</code><br />
Yes, it is that simple.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nasir.wordpress.com/43/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nasir.wordpress.com/43/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nasir.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nasir.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nasir.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nasir.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nasir.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nasir.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nasir.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nasir.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nasir.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nasir.wordpress.com/43/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nasir.wordpress.com&blog=50482&post=43&subd=nasir&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nasir.wordpress.com/2008/04/16/formattingstyling-will_paginate/feed/</wfw:commentRss>
		</item>
		<item>
		<title>SPhred Relaunched</title>
		<link>http://nasir.wordpress.com/2008/04/13/sphred-relaunched/</link>
		<comments>http://nasir.wordpress.com/2008/04/13/sphred-relaunched/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 20:12:43 +0000</pubDate>
		<dc:creator>nasir</dc:creator>
		
		<category><![CDATA[SP|hred.com]]></category>

		<category><![CDATA[captcha]]></category>

		<category><![CDATA[openid]]></category>

		<category><![CDATA[restful]]></category>

		<category><![CDATA[sphred]]></category>

		<guid isPermaLink="false">http://nasir.wordpress.com/?p=42</guid>
		<description><![CDATA[SPhred was relaunched last week (1st week of April, 200  with RESTful interface, new codebase and a new design. Here is a summary of the changes:

Now we have OpenID authentication on SPhred, i.e., new users do not need to Sign Up to use SPhred if they have an Open ID account.
In the past, some [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://www.sphred.com">SPhred</a> was relaunched last week (1st week of April, 200 <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> with <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">RESTful</a> interface, new codebase and a new design. Here is a summary of the changes:</p>
<ol>
<li>Now we have <a href="https://www.myopenid.com/">OpenID</a> authentication on SPhred, i.e., new users do not need to Sign Up to use SPhred if they have an Open ID account.</li>
<li>In the past, some users have suggested that they do not want their full name to be shown on their profile page, so now we do not show any user&#8217;s full name anywhere because we have added nicknames or user names.</li>
<li>I thought its a bit tedious to type your whole email address to login, hence added an option of using the Sign In name, ie.  you can either use your Sign In name or the email address. If you do not know your Sign In name yet then Sign In using your email address and you will see it once you are signed in.</li>
<li>If you have done some activity on SPhred like adding tags to your profile, SPhredding, connected with other people then SPhred can also recommend a network of people with similar interests on SPhred. Simply go to your profile and click on the &#8216;More Like you&#8217; link to check this out.</li>
<li>You can also send short messages to other users by using the &#8216;Buzz&#8217; link on a user profile.</li>
<li>I think it is important to bring the old posts back to users&#8217; attention once in a while even if it has gone past 10th page and hence have implemented a &#8216;Random Picks&#8217; widget which is shown on almost every page. Go to the widgets page <a href="http://www.sphred.com/widgets">http://www.sphred.com/widgets</a> and check out the other widgets.</li>
<li>I received lots of spam in the past, hence now we have <a href="http://en.wikipedia.org/wiki/Captcha">captcha</a> implemented on almost every page where users contribute to SPhred.</li>
<li>Previously, every post on SPhred had a URL like http://www.sphred.com/sphred_details/site_name,  this url scheme is still kept for backward compatibility but now we have better and more meaningful (&amp; RESTful) urls like http://www.sphred.com/categories/1-New-Launches/websites/site_name.NOTE: The old url scheme will be phased out eventually hence can I request you change the old urls on your blogs/websites to the new ones.</li>
<li>There are many more behind the scenes changes and some you will notice in time.</li>
<li>And yes, this is how SPhred looked in the past <a href="http://www.sphred.com/images/sphred.png">http://www.sphred.com/images/sphred.png</a></li>
</ol>
<p>If you have any feedback and suggestion then do send it to me, have fun!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nasir.wordpress.com/42/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nasir.wordpress.com/42/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nasir.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nasir.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nasir.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nasir.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nasir.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nasir.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nasir.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nasir.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nasir.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nasir.wordpress.com/42/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nasir.wordpress.com&blog=50482&post=42&subd=nasir&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nasir.wordpress.com/2008/04/13/sphred-relaunched/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Database views and Rails</title>
		<link>http://nasir.wordpress.com/2007/12/17/database-views-and-rails/</link>
		<comments>http://nasir.wordpress.com/2007/12/17/database-views-and-rails/#comments</comments>
		<pubDate>Mon, 17 Dec 2007 04:45:01 +0000</pubDate>
		<dc:creator>nasir</dc:creator>
		
		<category><![CDATA[Ruby On Rails]]></category>

		<category><![CDATA[database views]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://nasir.wordpress.com/2007/12/17/database-views-and-rails/</guid>
		<description><![CDATA[
When it comes to using database views then most of us think that it is difficult to use them with Rails or we may not be able to use ActiveRecord with it. Since I experimented with it in the past so I thought why not share it with you folks.
There are several situations when we [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><iframe src='http://digg.com/api/diggthis.php?u=http%3A%2F%2Fdigg.com%2Fprogramming%2FDatabase_views_and_Rails' height='82' width='55' frameborder='0' scrolling='no' style='float: right; margin-left: 10px; margin-bottom: 5px; padding: 4px 0 2px 4px; background: #fff;'></iframe></p>
<p>When it comes to using database views then most of us think that it is difficult to use them with Rails or we may not be able to use ActiveRecord with it. Since I experimented with it in the past so I thought why not share it with you folks.</p>
<p>There are several situations when we would like to use database views with our Rails application like:</p>
<p>Situation 1:</p>
<p>If we have a complex SQL query that we don&#8217;t want to code in your Rails app due to performance related or any other issues.</p>
<p>Situation 2:</p>
<p>When our database guys have already spent a great amount of time writing useful views that can be used in the current app</p>
<p>Situation 3:</p>
<p>When we have some repeated query to run in every select statement to fetch data from every table. For instance<br />
<code>Select * from table_name where created_on &lt; today;</code></p>
<p>In that case we would prefer to have a view with this query and call that view.</p>
<p>In all the above situations, it is pretty easy to use our database views with Rails.</p>
<p>How?</p>
<p>Simply create your model as you would for a table and it will work fine just like your model for a database table.</p>
<p><strong>Interesting thing is, apart from viewing data using views I can even update, delete and insert data in my database tables using my views with all those ActiveRecord/ORM niceties (in Sybase) as long as each database view consists of only one table</strong>. If you are using more than one table in your database views then you can only view data but cannot do delete, update and insert operation.</p>
<p>I think this is really useful if we have a database that do not conform to Rails conventions but we would like to follow those conventions without changing the current database if other applications depend on it as well.  Simply create views for each table in the database and use those views for our models instead of actual tables.</p>
<p>Those single table views (in other words updatable views) will allow us to follow Rails database conventions without changing our existing database tables while also keeping our database guys happy ;o)</p>
<p>(<a href="http://nasir.wordpress.com/2007/10/24/not-following-rails-table-and-field-conventions/">Though we also have other options if we are not following rails conventions</a>)</p>
<p><strong>NOTE: I have tried updatable views only with Sybase and would love to know about your experience with other databases.</strong></p>
<hr size="1" /><strong>Sign up and be a part of <a href="http://www.sphred.com" title="Spread It | Shred It" target="_blank">SPhred.com</a> and don&#8217;t forget to invite your friends ;o)</strong><em> </em><br />
<hr size="1" />
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nasir.wordpress.com/41/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nasir.wordpress.com/41/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nasir.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nasir.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nasir.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nasir.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nasir.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nasir.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nasir.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nasir.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nasir.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nasir.wordpress.com/41/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nasir.wordpress.com&blog=50482&post=41&subd=nasir&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nasir.wordpress.com/2007/12/17/database-views-and-rails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Stored Procedures and Rails - Part 2</title>
		<link>http://nasir.wordpress.com/2007/12/04/stored-procedures-and-rails-part-2/</link>
		<comments>http://nasir.wordpress.com/2007/12/04/stored-procedures-and-rails-part-2/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 04:53:25 +0000</pubDate>
		<dc:creator>nasir</dc:creator>
		
		<category><![CDATA[Ruby On Rails]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[ruby]]></category>

		<category><![CDATA[stored procedures]]></category>

		<guid isPermaLink="false">http://nasir.wordpress.com/2007/12/04/stored-procedures-and-rails-part-2/</guid>
		<description><![CDATA[
Continued from Stored Procedures and Rails - Part 1
Now we know how to call our good and reliable stored procedures but there is a difference when we get records from database using Rails ORM/ActiveRecord compared to a stored procedure. This requires a different approach to access records returned from a stored procedure.
Lets say we have [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><iframe src='http://digg.com/api/diggthis.php?u=http%3A%2F%2Fdigg.com%2Fprogramming%2FStored_Procedures_and_Rails_Part_2' height='82' width='55' frameborder='0' scrolling='no' style='float: right; margin-left: 10px; margin-bottom: 5px; padding: 4px 0 2px 4px; background: #fff;'></iframe><br />
<a href="http://nasir.wordpress.com/2007/12/03/stored-procedures-and-rails/">Continued from Stored Procedures and Rails - Part 1</a></p>
<p>Now we know how to call our good and reliable stored procedures but there is a difference when we get records from database using Rails ORM/ActiveRecord compared to a stored procedure. This requires a different approach to access records returned from a stored procedure.</p>
<p>Lets say we have a table orders and a corresponding model Order. We all know, how to access data using ActiveRecord, eg.</p>
<pre>
@orders = Order.find(:all, :conditions =&gt; ["some simple condition"])</pre>
<p>then what is returned to us is an array of order objects which we can access by either using a block or a for loop. For instance,</p>
<pre>
for order in @orders
  puts order.order_name
end</pre>
<p>but this is not possible in case if we use a stored procedure to get data on orders because we don&#8217;t get back an array of order objects instead we simply get back an array of hashes, ie. an array of records with their attributes and values. Hence, the above code will not work.<br />
Lets say you call your stored procedure, like this</p>
<pre>
@orders = CallStoredProcedure.fetch_records("exec procedure_name 'args1', 'args2'")</pre>
<p>as mentioned in the <a href="http://nasir.wordpress.com/2007/12/03/stored-procedures-and-rails/">previous post</a>.<br />
Then you need to access records with a little modification within your for loop</p>
<pre>
for order in @orders
  puts order['order_name']
end</pre>
<p>I thought this might save sometime who are new to using stored procedures.</p>
<p>NOTE: The only downside of using stored procedures in Rails currently is; you cannot use the Object Relational Mapping which you might have been used to by know, ie. picking all children of an order by simply typing</p>
<pre>
order.children</pre>
<p>The best thing to do is to use stored procedures judiciously and mainly for complex logic/calculation in your database but when you want to do something simple with your data like simply showing something or a quick update then use ORM over stored procedures.</p>
<hr size="1" /><strong>Sign up and be a part of <a href="http://www.sphred.com" title="Spread It | Shred It" target="_blank">SPhred.com</a> and don&#8217;t forget to invite your friends ;o)</strong><em> </em></p>
<hr size="1" />
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nasir.wordpress.com/40/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nasir.wordpress.com/40/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nasir.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nasir.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nasir.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nasir.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nasir.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nasir.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nasir.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nasir.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nasir.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nasir.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nasir.wordpress.com&blog=50482&post=40&subd=nasir&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nasir.wordpress.com/2007/12/04/stored-procedures-and-rails-part-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Stored Procedures and Rails - Part 1</title>
		<link>http://nasir.wordpress.com/2007/12/03/stored-procedures-and-rails/</link>
		<comments>http://nasir.wordpress.com/2007/12/03/stored-procedures-and-rails/#comments</comments>
		<pubDate>Mon, 03 Dec 2007 20:23:06 +0000</pubDate>
		<dc:creator>nasir</dc:creator>
		
		<category><![CDATA[Ruby On Rails]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[ruby]]></category>

		<category><![CDATA[stored procedures]]></category>

		<guid isPermaLink="false">http://nasir.wordpress.com/2007/12/03/stored-procedures-and-rails/</guid>
		<description><![CDATA[
There is no documentation in Rails about how to use stored procedures or atleast it is difficult to find where to look at. And according to DHH stored procedures is not the Rails Way to do things. DHH mentioned in his post
I want a single layer of cleverness: My domain model. Object-orientation is all about [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><iframe src='http://digg.com/api/diggthis.php?u=http%3A%2F%2Fdigg.com%2Fprogramming%2FUsing_stored_procedures_in_rails' height='82' width='55' frameborder='0' scrolling='no' style='float: right; margin-left: 10px; margin-bottom: 5px; padding: 4px 0 2px 4px; background: #fff;'></iframe><br />
There is no documentation in Rails about how to use stored procedures or atleast it is difficult to find where to look at. And according to <a href="http://web.archive.org/web/20060418215514/http://www.loudthinking.com/arc/000516.html" target="_blank">DHH</a> stored procedures is not the <strong>Rails Way</strong> to do things. DHH mentioned in his post</p>
<blockquote><p>I want a single layer of cleverness: My domain model. Object-orientation is all about encapsulating clever. Letting it sieve half ways through to the database is a terrible violation of those fine intentions. And I want no part of it.</p></blockquote>
<p>I somewhat agree to what DHH has to say. And that&#8217;s all fine but what if we don&#8217;t have any other option apart from using stored procedure and our stored procedure has some complex logic which we don&#8217;t want to put in our Ruby code then here is a little hack to it</p>
<pre>
ActiveRecord::Base.connection.execute("execute procedure_name")</pre>
<p>But the above line will not return anything it will only execute your procedure, considering everything went OK.</p>
<p>Moreover, it&#8217;s not DRY if everytime you have to write that line of code just to call a procedure.</p>
<p>Here is a better way to do it:</p>
<p>1) Create a class and let&#8217;s call it</p>
<pre>
Class CallStoredProcedures
  your methods here .......
end</pre>
<p>2) Create a class method within it</p>
<pre>
def self.fetch_db_records(proc_name_with_parameters)
  ActiveRecord::Base.connection.execute(proc_name_with_parameters)
end</pre>
<p>Then simply call it as</p>
<pre>
CallStoredProcedures.fetch_db_records("exec procedure_name 'args1', 'args2', 'and so on' ")</pre>
<p>Now you might say, that this will only execute a procedure (as I mentioned above) what if I want to return records or values from stored procedures. Then I will prefer class methods and this is how I would do it,</p>
<pre>
class CallStoredProcedures &lt; ActiveRecord::Base

  def self.fetch_db_records(proc_name_with_parameters)
    connection.select_all(proc_name_with_parameters)
  end

  def self.insert_update_delete_calculate(proc_name_with_parameters)
    connection.execute(proc_name_with_parameters)
  end

  def self.fetch_val_from_sp(proc_name_with_parameters)
    connection.select_value(proc_name_with_parameters)
  end

end</pre>
<p>And then simply call it like</p>
<pre>
CallStoredProcedures.method_name("exec procedure_name 'args1', 'args2', 'and so on' ")</pre>
<p>Sweet&#8230;</p>
<p>If you know a better way to do it then I would love to know your views.</p>
<p><a href="http://nasir.wordpress.com/2007/12/04/stored-procedures-and-rails-part-2/">Stored Procedures and Rails - Part 2 </a></p>
<hr size="1" /><strong>Sign up and be a part of <a href="http://www.sphred.com" title="Spread It | Shred It" target="_blank">SPhred.com</a> and don&#8217;t forget to invite your friends ;o)</strong><em> </em><br />
<hr size="1" />
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nasir.wordpress.com/39/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nasir.wordpress.com/39/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nasir.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nasir.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nasir.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nasir.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nasir.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nasir.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nasir.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nasir.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nasir.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nasir.wordpress.com/39/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nasir.wordpress.com&blog=50482&post=39&subd=nasir&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nasir.wordpress.com/2007/12/03/stored-procedures-and-rails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>RSS Feeds + Ruby on Rails + Web App</title>
		<link>http://nasir.wordpress.com/2007/12/03/rss-feeds-ruby-on-rails-web-app/</link>
		<comments>http://nasir.wordpress.com/2007/12/03/rss-feeds-ruby-on-rails-web-app/#comments</comments>
		<pubDate>Mon, 03 Dec 2007 05:34:06 +0000</pubDate>
		<dc:creator>nasir</dc:creator>
		
		<category><![CDATA[Ruby On Rails]]></category>

		<category><![CDATA[feeds]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[rss]]></category>

		<category><![CDATA[ruby]]></category>

		<category><![CDATA[web app]]></category>

		<guid isPermaLink="false">http://nasir.wordpress.com/2007/12/03/rss-feeds-ruby-on-rails-web-app/</guid>
		<description><![CDATA[
The Feedtools library is a very comprehensive and powerful Ruby library for handling rss, atom, etc as well as caching. It makes creating, consuming and manipulating feeds a piece of cake.
It’s ideal for parsing RSS feeds in Ruby on Rails applications.
Assuming that you already have installed Ruby and Rubygem, simply run
gem install feedtools
to install the [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><iframe src='http://digg.com/api/diggthis.php?u=http%3A%2F%2Fdigg.com%2Fprogramming%2FHow_to_show_RSS_feeds_in_a_Ruby_on_Rails_Apps' height='82' width='55' frameborder='0' scrolling='no' style='float: right; margin-left: 10px; margin-bottom: 5px; padding: 4px 0 2px 4px; background: #fff;'></iframe><br />
The <a href="http://sporkmonger.com/projects/feedtools/" target="_blank">Feedtools</a> library is a very comprehensive and powerful Ruby library for handling rss, atom, etc as well as caching. It makes creating, consuming and manipulating feeds a piece of cake.</p>
<p>It’s ideal for parsing RSS feeds in Ruby on Rails applications.</p>
<p>Assuming that you already have installed Ruby and Rubygem, simply run</p>
<pre>gem install feedtools</pre>
<p>to install the FeedTools gem</p>
<p>or download it from the <a href="http://rubyforge.org/frs/?group_id=775" target="_blank">FeedTools project page</a> and put the untar/unzipped folder in the vendor/plugins directory of your app.</p>
<p>To do a very quick test, create a file called testfeed.rb and add the below code in it:</p>
<pre>
    require "RubyGems"
    require "feed_tools"

    feedurls = 'http://www.sphred.com/combined_feed'
    # If you want to fetch more than one feed then comment the above feedurls variable and   uncomment the below one.
    #feedurls = %w(http://feeds.feedburner.com/Sphred_top_10_feeds   http://feeds.feedburner.com/Sphred_site_only http://feeds.feedburner.com/Sphred_site_feature)
    my_feeds = FeedTools::build_merged_feed feedurls

    my_feeds.title = 'Sphred.com Feed'
    my_feeds.copyright = 'SPhred'
    my_feeds.author = 'Nasir '
    my_feeds.id = "http://www.sphred.com/combined_feed"

    File.open('./my_feeds.xml', 'w') do |file|
      file.puts my_feeds.build_xml()
    end

    puts "Checkout Sphred.com while I fetct feeds for you"</pre>
<p>Running this file from IRB will create a my_feeds.xml file in your current directory with all the feed contents.</p>
<p>To show feeds on a website, you need to do a few things:<br />
1) Put</p>
<pre>require "feed_tools"</pre>
<p>at the top of your controller<br />
2) Create an action within this controller to show your feeds, lets call that action user_data</p>
<pre>
    def user_data
      @feed = FeedTools::Feed.open(params[:feed_url])
      # You can first test it with a static feed url like this
      #@feed = FeedTools::Feed.open(&#8217;http://www.sphred.com/combined_feed&#8217;)
    end</pre>
<p>3) In the corresponding view add this code</p>
<pre>
  &lt;div class="feeds"&gt;
    &lt;h3&gt;
      &lt;a href="&lt;%= h @feed.link %&gt;"&gt;
      &lt;%= @feed.title %&gt;&lt;/a&gt;
    &lt;/h3&gt;
    &lt;p&gt;&lt;%= @feed.description %&gt;&lt;/p&gt;
    &lt;% for feed in @feed.items %&gt;
      &lt;div class="feed_item"&gt;
        &lt;h4&gt;
          &lt;a href="&lt;%= h feed.link %&gt;"&gt;
          &lt;%= feed.title %&gt;&lt;/a&gt;
        &lt;/h4&gt;
        &lt;p&gt;&lt;%= feed.description %&gt;&lt;/p&gt;
     &lt;/div&gt;
   &lt;% end %&gt;
 &lt;/div&gt;</pre>
<p>This is all good for development environment or for small apps but once you go to production environment then you have to think of caching to avoid hitting the feed server every time before displaying feed contents.</p>
<hr size="1" /><strong>Sign up and be a part of <a href="http://www.sphred.com" title="Spread It | Shred It" target="_blank">SPhred.com</a> and don&#8217;t forget to invite your friends ;o)</strong><em> </em></p>
<hr size="1" />
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nasir.wordpress.com/38/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nasir.wordpress.com/38/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nasir.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nasir.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nasir.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nasir.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nasir.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nasir.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nasir.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nasir.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nasir.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nasir.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nasir.wordpress.com&blog=50482&post=38&subd=nasir&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nasir.wordpress.com/2007/12/03/rss-feeds-ruby-on-rails-web-app/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Extreme Programming - xp</title>
		<link>http://nasir.wordpress.com/2007/11/16/extreme-programming-xp/</link>
		<comments>http://nasir.wordpress.com/2007/11/16/extreme-programming-xp/#comments</comments>
		<pubDate>Fri, 16 Nov 2007 06:19:26 +0000</pubDate>
		<dc:creator>nasir</dc:creator>
		
		<category><![CDATA[Ruby On Rails]]></category>

		<category><![CDATA[extreme programming]]></category>

		<category><![CDATA[xp]]></category>

		<guid isPermaLink="false">http://nasir.wordpress.com/2007/11/16/extreme-programming-xp/</guid>
		<description><![CDATA[I prepared a mind map based on Kent Beck&#8217;s book. Extreme Programming - mind map.
I will be updating this post with a new mind map and some write  up in the next few days, so check back soon.
Sign up and be a part of SPhred.com and don&#8217;t forget to invite your friends ;o) 

 [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I prepared a mind map based on Kent Beck&#8217;s book. <a href="http://nasir.files.wordpress.com/2007/11/xp.png" title="Extreme Programming - click here to view mind map">Extreme Programming - mind map.</a></p>
<p>I will be updating this post with a new mind map and some write  up in the next few days, so check back soon.</p>
<hr size="1" /><strong>Sign up and be a part of <a href="http://www.sphred.com" title="Spread It | Shred It" target="_blank">SPhred.com</a> and don&#8217;t forget to invite your friends ;o)</strong><em> </em><br />
<hr size="1" />
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nasir.wordpress.com/36/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nasir.wordpress.com/36/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nasir.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nasir.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nasir.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nasir.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nasir.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nasir.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nasir.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nasir.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nasir.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nasir.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nasir.wordpress.com&blog=50482&post=36&subd=nasir&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nasir.wordpress.com/2007/11/16/extreme-programming-xp/feed/</wfw:commentRss>
		</item>
		<item>
		<title>New features on SPhred.com</title>
		<link>http://nasir.wordpress.com/2007/11/05/new-features-on-sphred/</link>
		<comments>http://nasir.wordpress.com/2007/11/05/new-features-on-sphred/#comments</comments>
		<pubDate>Mon, 05 Nov 2007 02:38:39 +0000</pubDate>
		<dc:creator>nasir</dc:creator>
		
		<category><![CDATA[SP|hred.com]]></category>

		<category><![CDATA[add photo]]></category>

		<category><![CDATA[new features]]></category>

		<category><![CDATA[search]]></category>

		<category><![CDATA[sphred]]></category>

		<guid isPermaLink="false">http://nasir.wordpress.com/2007/11/05/new-features-on-sphred/</guid>
		<description><![CDATA[
First, A big thanks to all the guys out there for registering and sending invitation to their friends/relatives to try out sphred.com
SPhred is currently in very early stages, like what other sites say &#8216;in beta&#8217;.
A few things are added on SPhred in the last couple of days:
1) Earlier you first had to create a post [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><iframe src='http://digg.com/api/diggthis.php?u=http%3A%2F%2Fdigg.com%2Ftech_news%2FSphred_com_has_launched_some_new_features%2Fblog' height='82' width='55' frameborder='0' scrolling='no' style='float: right; margin-left: 10px; margin-bottom: 5px; padding: 4px 0 2px 4px; background: #fff;'></iframe></p>
<p>First, A big thanks to all the guys out there for registering and sending invitation to their friends/relatives to try out <a href="http://www.sphred.com">sphred.com</a></p>
<p>SPhred is currently in very early stages, like what other sites say &#8216;in beta&#8217;.</p>
<p>A few things are added on SPhred in the last couple of days:</p>
<p>1) Earlier you first had to create a post and then could add tags to it but now you can add tags while creating a post.</p>
<p>2) Now you can put a face to your name i.e. add a picture. This functionality is available by going to your profile page. The option is given just below &#8220;Profile for your name&#8221;. You can access your profile page after signing in by clicking on your email address part of &#8216;Signed in as your <strong>email address</strong>&#8216;.</p>
<p>3) Page to view users who sphredded an item has been redone and looks much better now (atleast I think so). Have a look by clicking on the number of users who sphreaded an item. Or you can directly go here <a href="http://sphred.com/users_sphredding/9-tutsbuzzcom">http://sphred.com/users_sphredding/9-tutsbuzzcom</a></p>
<p>4) Search is functional now though currently it only looks for the search term in a Sphred Item&#8217;s title or description.</p>
<p>6) Comments look much better now as you can see the picture of people who posted comments.</p>
<p>I would love to hear your feedback or suggestions i.e. what you would like to have on sphred and what you think isn&#8217;t working or won&#8217;t work.</p>
<p>Once again thank you all for using sphred.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nasir.wordpress.com/34/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nasir.wordpress.com/34/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nasir.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nasir.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nasir.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nasir.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nasir.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nasir.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nasir.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nasir.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nasir.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nasir.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nasir.wordpress.com&blog=50482&post=34&subd=nasir&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nasir.wordpress.com/2007/11/05/new-features-on-sphred/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Not binding your selection list to a particular model in Rails</title>
		<link>http://nasir.wordpress.com/2007/11/02/not-binding-your-selection-list-to-a-particular-model-in-rails/</link>
		<comments>http://nasir.wordpress.com/2007/11/02/not-binding-your-selection-list-to-a-particular-model-in-rails/#comments</comments>
		<pubDate>Fri, 02 Nov 2007 09:23:09 +0000</pubDate>
		<dc:creator>nasir</dc:creator>
		
		<category><![CDATA[Ruby On Rails]]></category>

		<category><![CDATA[collection_select]]></category>

		<category><![CDATA[drop down box]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[ruby]]></category>

		<category><![CDATA[selection list]]></category>

		<category><![CDATA[select_tag]]></category>

		<guid isPermaLink="false">http://nasir.wordpress.com/2007/11/02/not-binding-your-selection-list-to-a-particular-model-in-rails/</guid>
		<description><![CDATA[
In Rails, we use collection_select helper to display values from a particular model using a drop down  but it is generally tied to a particular model, i.e. if we look at the generated html then we see something like

select name=&#8221;customer[customer_search]&#8221; id=&#8221;customer_customer_search&#8221;

What if we want something like:

select name=&#8221;customer_search&#8221; id=&#8221;customer_search&#8221;

Then we can simply use select_tag helper [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><iframe src='http://digg.com/api/diggthis.php?u=http%3A%2F%2Fdigg.com%2Fprogramming%2FNot_binding_your_selection_list_to_a_particular_model_in_Rails' height='82' width='55' frameborder='0' scrolling='no' style='float: right; margin-left: 10px; margin-bottom: 5px; padding: 4px 0 2px 4px; background: #fff;'></iframe><br />
In Rails, we use collection_select helper to display values from a particular model using a drop down  but it is generally tied to a particular model, i.e. if we look at the generated html then we see something like<br />
<code><br />
select name=&#8221;customer[customer_search]&#8221; id=&#8221;customer_customer_search&#8221;<br />
</code><br />
What if we want something like:<br />
<code><br />
select name=&#8221;customer_search&#8221; id=&#8221;customer_search&#8221;<br />
</code><br />
Then we can simply use select_tag helper and get the values from our model like this<br />
<code><br />
customers = Customer.find(:all, :order =&gt; &#8216;first_name&#8217;).map{|x| [x.full_name] + [x.id]}<br />
select_tag(:customer_search, options_for_select(customers))<br />
</code><br />
If we don&#8217;t want to display the values from model and would like to feed in something simple and manually, then<br />
<code><br />
customers = [['text_to_display1', 'value1'], ['text_to_display2', 'value2']]<br />
select_tag(:customer_search, options_for_select(customers))<br />
</code><br />
If text_to_display and its value both are same then we can use something as simple as:<br />
<code><br />
customers = ['wee', 'asdf']<br />
select_tag(:customer_search, options_for_select(customers))<br />
</code></p>
<hr size="1" /><strong>Sign up and be a part of <a href="http://www.sphred.com" title="Spread It | Shred It" target="_blank">SPhred.com</a> and don&#8217;t forget to invite your friends ;o)</strong><em> </em></p>
<hr size="1" />
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nasir.wordpress.com/33/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nasir.wordpress.com/33/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nasir.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nasir.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nasir.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nasir.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nasir.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nasir.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nasir.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nasir.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nasir.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nasir.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nasir.wordpress.com&blog=50482&post=33&subd=nasir&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nasir.wordpress.com/2007/11/02/not-binding-your-selection-list-to-a-particular-model-in-rails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Pagination in Ruby On Rails using will_paginate plugin</title>
		<link>http://nasir.wordpress.com/2007/10/31/pagination-in-ruby-on-rails-using-will_paginate-plugin/</link>
		<comments>http://nasir.wordpress.com/2007/10/31/pagination-in-ruby-on-rails-using-will_paginate-plugin/#comments</comments>
		<pubDate>Wed, 31 Oct 2007 06:15:03 +0000</pubDate>
		<dc:creator>nasir</dc:creator>
		
		<category><![CDATA[Ruby On Rails]]></category>

		<category><![CDATA[pagination]]></category>

		<category><![CDATA[plugin]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[ruby]]></category>

		<category><![CDATA[will_paginate]]></category>

		<guid isPermaLink="false">http://nasir.wordpress.com/2007/10/31/pagination-in-ruby-on-rails-using-will_paginate-plugin/</guid>
		<description><![CDATA[
We do pagination in Rails by using its built in paginate method in our controller

def display_data
@display_data_pages, @display_data = paginate(:location_data,
:conditions =&#62; ["loc.updated_at &#60; ?", params[:date]],
:joins =&#62; &#8220;as loc inner join location_address as locadd on loc.id = locadd.id&#8221;
:per_page =&#62; 10,
:order =&#62; &#8220;location_name&#8221;)
end

Often we would like to put this code inside our model mainly for two reasons:
1) Code [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><iframe src='http://digg.com/api/diggthis.php?u=http%3A%2F%2Fdigg.com%2Fprogramming%2FPagination_in_Ruby_On_Rails_using_will_paginate_plugin' height='82' width='55' frameborder='0' scrolling='no' style='float: right; margin-left: 10px; margin-bottom: 5px; padding: 4px 0 2px 4px; background: #fff;'></iframe><br />
We do pagination in Rails by using its built in paginate method in our controller</p>
<p><code><br />
def display_data<br />
@display_data_pages, @display_data = paginate(:location_data,<br />
:conditions =&gt; ["loc.updated_at &lt; ?", params[:date]],<br />
:joins =&gt; &#8220;as loc inner join location_address as locadd on loc.id = locadd.id&#8221;<br />
:per_page =&gt; 10,<br />
:order =&gt; &#8220;location_name&#8221;)<br />
end<br />
</code><br />
Often we would like to put this code inside our model mainly for two reasons:<br />
1) Code Reuse, and<br />
2) To keep the business logic where it belongs<br />
But we couldn&#8217;t do that (since it is difficult to do) due to the paginate method and had to write the same code more than once in the controller and sometimes in more than one controller.<br />
On top of that we have to put this chunk of code in our views only to show the pagination links<br />
<code>&lt;div class="pagination"&gt;<br />
&lt;%= if @survdefault_pages.current.previous<br />
link_to(&#8221;&lt; Prev&#8221;, { :page =&gt; @survdefault_pages.current.previous })<br />
end %&gt;</code><br />
<code><br />
&lt;%= pagination_links(@survdefault_pages) %&gt;<br />
</code><code><br />
&lt;%= if @survdefault_pages.current.next<br />
link_to(&#8221;Next &gt;&#8221;, { :page =&gt; @survdefault_pages.current.next})<br />
end %&gt;<br />
&lt;/div&gt;<br />
</code><br />
We can put this code inside a partial but it is still a pain.</p>
<p>Moreover, Rails&#8217; built in pagination will be out of Rails before it will reach version 2 which is not that far.</p>
<p><strong>Update: Rails built in pagination is now removed but is still available as a plugin, ie, classic_pagination.</strong></p>
<p>We have another option, use will_paginate plugin. The advantages of will_paginate plugin are:<br />
1) Easy to use,<br />
2) Less code in views to show pagination links, and<br />
3) It can be used inside models</p>
<p><strong>Follow these steps to use will_paginate plugin </strong></p>
<p>1) Grab it, by going to the command prompt and change directory to the app root. Then run<br />
<code>ruby script/plugin install svn://errtheblog.com/svn/plugins/will_paginate</code><br />
<strong>NOTE</strong> If the above code doesn&#8217;t installs the plugin in your vendor/plugins directory then you probably don&#8217;t have svn installed on your machine.</p>
<p>2) Now in our controller we can simply write<br />
<code><br />
def display_data<br />
@display_data = LocationData.paginate(:page =&gt; params[:page],<br />
:conditions =&gt; ["loc.updated_at &lt; ?", params[:date]],<br />
:joins =&gt; &#8220;as loc inner join location_address as locadd on loc.id = locadd.id&#8221;, :per_page =&gt; 10,<br />
:order =&gt; &#8220;location_name&#8221;)<br />
end<br />
</code><br />
:per_page and :page are required.<br />
The best thing is, as mentioned above, we can move this code to the LocationData model that will help in keeping our controller clean and will also help in code reuse.<br />
To do that, we can create a class method in our model like:<br />
<code><br />
def self.display_location_data(update_date, page)<br />
paginate(:page =&gt; page, :conditions =&gt; ["loc.updated_at &lt;  ?", update_date], :joins =&gt; &#8220;as loc inner join location_address as locadd on loc.id = locadd.id&#8221;,<br />
:per_page =&gt; 10,<br />
:order =&gt; &#8220;location_name&#8221;)<br />
end<br />
</code><br />
In our controller, the display_data method will now be replaced with just one line<br />
<code><br />
def display_data<br />
@display_data = LocationData.display_location_data(params[:date], params[:page])<br />
end<br />
</code><br />
In the view, the big chunk of code will be replaced by a single line to show our pagination links, like<br />
<code><br />
&lt;%= will_paginate @display_data %&gt; </code></p>
<p>And we are done&#8230;</p>
<p>UPDATE: <a href="http://nasir.wordpress.com/2008/04/16/formattingstyling-will_paginate">Formatting / Styling pagination links</a></p>
<hr size="1" /><strong>Sign up and be a part of <a href="http://www.sphred.com" title="Spread It | Shred It" target="_blank">SPhred.com</a> and don&#8217;t forget to invite your friends ;o)</strong><em> </em></p>
<hr size="1" />
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nasir.wordpress.com/32/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nasir.wordpress.com/32/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nasir.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nasir.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nasir.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nasir.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nasir.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nasir.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nasir.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nasir.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nasir.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nasir.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nasir.wordpress.com&blog=50482&post=32&subd=nasir&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nasir.wordpress.com/2007/10/31/pagination-in-ruby-on-rails-using-will_paginate-plugin/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>