<?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:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ph-lee &#187; how-to</title>
	<atom:link href="http://www.ph-lee.com/tag/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ph-lee.com</link>
	<description>aspire to inspire before you expire</description>
	<lastBuildDate>Sat, 29 May 2010 19:11:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to : Exceptions in ruby (on rails) coming from a Java or C# .NET background</title>
		<link>http://www.ph-lee.com/2009/07/14/how-to-exceptions-in-ruby-on-rails-coming-from-a-java-or-c-net-background/</link>
		<comments>http://www.ph-lee.com/2009/07/14/how-to-exceptions-in-ruby-on-rails-coming-from-a-java-or-c-net-background/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 13:25:54 +0000</pubDate>
		<dc:creator>ph-lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[begin]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[catch]]></category>
		<category><![CDATA[end]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[raise]]></category>
		<category><![CDATA[rescue]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[throw]]></category>
		<category><![CDATA[try]]></category>

		<guid isPermaLink="false">http://www.ph-lee.com/?p=114</guid>
		<description><![CDATA[Personally coming from a Java and C# .NET background exceptions in ruby (on rails) seemed primative and rather lacking compared to the former languages I have had experiences in. To show off the differences and to show how to do exceptions in ruby lets start of with some examples.
C# .NET Example
Class (User)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// &#60;summary&#62;
/// constructor for [...]]]></description>
			<content:encoded><![CDATA[<p>Personally coming from a Java and C# .NET background exceptions in ruby (on rails) seemed primative and rather lacking compared to the former languages I have had experiences in. To show off the differences and to show how to do exceptions in ruby lets start of with some examples.</p>
<p>C# .NET Example<br />
Class (User)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// constructor for creating a new user</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;str_username&quot;&gt;username of the user&lt;/param&gt;</span>
&nbsp;
<span style="color: #0600FF;">public</span> User<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> str_username<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">//check method parameters/arguments for null or empty</span>
&nbsp;
   <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>str_username<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//throw an exception since parameters entered are not valid</span>
      <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;argument str_username was null or empty&quot;</span>, <span style="color: #666666;">&quot;str_username&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">str_username</span> <span style="color: #008000;">=</span> str_username<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// absurd method for the purpose of this example</span>
<span style="color: #008080; font-style: italic;">/// creates two users</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> createBobAndEmpty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">//try and create bob</span>
&nbsp;
   <span style="color: #0600FF;">try</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008000;">new</span> User<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Bob&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
   <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ArgumentException e<span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//this exception will not be thrown in this case</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//try and create empty</span>
&nbsp;
   <span style="color: #0600FF;">try</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008000;">new</span> User<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
   <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ArgumentException e<span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//this exception will be thrown in this case</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">//e is the variable storing the exception which we can print the exception message, variable of concern</span>
      <span style="color: #008080; font-style: italic;">//in other cases the entire stack trace can be printed for debugging purposes</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">//in this particular case it would be best to log the error and simply notify the user they can't create a user with empty string as username</span>
   <span style="color: #000000;">&#125;</span>
   <span style="color: #0600FF;">finally</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//this finally block is optional and normally used for garbage collection/closing connections/closing files etc</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Java Example<br />
Class (User)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * constructor for creating a new User object
 *
 * @param str_username the username of the user
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> User<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> str_username<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">//check parameter is not null</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>str_username <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">NullPointerException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;str_username is null&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//check parameter is not an empty string</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>str_username.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;str_username is empty&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">str_username</span> <span style="color: #339933;">=</span> str_username<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * absurd method for the purpose of this example
 * creates 2 users
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> createBobAndEmpty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">//try and create the user bob</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">try</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bob&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NullPointerException</span> e<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this exception will not be thrown in this case</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalArgumentException</span> e<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this exception will not be thrown in this case</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//try and create user empty</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">try</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NullPointerException</span> e<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this exception will not be thrown in this case</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalArgumentException</span> e<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this exception will be thrown in this case</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//e is the variable storing the exception which we can print the exception message, variable of concern</span>
      <span style="color: #666666; font-style: italic;">//in other cases the entire stack trace can be printed for debugging purposes</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//in this particular case it would be best to log the error and simply notify the user they can't create a user with empty string as username</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">finally</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this finally block is optional and normally used for garbage collection/closing connections/closing files etc</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Ruby</p>
<p>Firstly Ruby uses different terms/keywords compared to Java or C#, the usual try/catch/throw/finally are different. Instead&#8230;</p>
<ul>
<li>&#8220;try&#8221; becomes &#8220;begin&#8221;</li>
<li>&#8220;catch&#8221; becomes &#8220;rescue&#8221;</li>
<li>&#8220;finally&#8221; becomes &#8220;ensure&#8221;</li>
</ul>
<p>So in Ruby<br />
Class (User)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#class constructor for a user</span>
<span style="color:#008000; font-style:italic;">#username represents the username for the new user</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>username<span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#008000; font-style:italic;">#check argument username is not an empty string</span>
   <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span> <span style="color:#9966CC; font-weight:bold;">if</span> username.<span style="color:#9900CC;">empty</span>? <span style="color:#008000; font-style:italic;">#keyword &quot;raised&quot; used instead of &quot;throw&quot;</span>
&nbsp;
   <span style="color:#0066ff; font-weight:bold;">@username</span> = username
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#absurd method for the purpose of this article</span>
<span style="color:#008000; font-style:italic;">#create 2 users</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> User.<span style="color:#9900CC;">create_bob_and_empty</span>
   <span style="color:#008000; font-style:italic;">#try and create user user Bob</span>
&nbsp;
   <span style="color:#9966CC; font-weight:bold;">begin</span> <span style="color:#008000; font-style:italic;">#this keyword is used instead of &quot;try&quot;</span>
      User.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Bob&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      <span style="color:#008000; font-style:italic;">#this rescue clause will not be performed in this example</span>
   <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#008000; font-style:italic;">#this clause will be performed since none of the above rescue clauses were fired</span>
      <span style="color:#008000; font-style:italic;">#can be used to report success of operations where no exceptions were encountered</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
   <span style="color:#008000; font-style:italic;">#try and create user empty</span>
&nbsp;
   <span style="color:#9966CC; font-weight:bold;">begin</span>
      User.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Bob&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      <span style="color:#008000; font-style:italic;">#this clause will be fired in this case</span>
&nbsp;
      <span style="color:#008000; font-style:italic;">#the variable e holds the exception that has been raised and can be used to trace exceptions stack</span>
   <span style="color:#9966CC; font-weight:bold;">ensure</span>
      <span style="color:#008000; font-style:italic;">#this clause will be fired in this case</span>
      <span style="color:#008000; font-style:italic;">#this is equivalent to the finally cluse in java or csharp</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>So that completes the examples, now you can compare the differences between ruby and the other languages. The <a href="http://blog.nicksieger.com/articles/2006/09/06/rubys-exception-hierarchy">ruby exception hierarchy [Sieger]</a> in comparisonm to Java and C# is rather small and limited and it is likely that you will need to <a href="http://rpheath.com/posts/237-raising-custom-exceptions-in-rails">create your own exceptions [Heath]</a> by extending the hierarchy in your application. Before reading Heath&#8217;s article I should warn you his examples are poor as he uses exception for normal control-flow of logic rather than using exception for error handling which what it should only be used for.</p>
<p>Another thing you may also find useful is the keyword &#8220;retry&#8221;. This word/statement can be put in any rescue clause to restart the begin/end block which in lies in. This maybe useful to re-attempt to parse a file or connect to databse etc.</p>
<p>In ruby on top of begin/raise/rescue/end, throw/catch can also be used. Here&#8217;s an example&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">catch</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:done</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
   a = <span style="color:#006666;">1</span>
   <span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#0000FF; font-weight:bold;">true</span>
      throw <span style="color:#ff3333; font-weight:bold;">:done</span> <span style="color:#9966CC; font-weight:bold;">if</span> a == <span style="color:#006666;">100</span>
      a<span style="color:#006600; font-weight:bold;">++</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>In ruby, throw does not throw an exception instead it throws a string or :symbol. So as far I can tell it is only used for control-flow purposes rather than the classical case of exception handling, unless someone can tell me otherwise.</p>
<p>Hope you found this article useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ph-lee.com/2009/07/14/how-to-exceptions-in-ruby-on-rails-coming-from-a-java-or-c-net-background/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to : Use Ruby on Rails ActiveRecord to query multiple tables with joins and conditions</title>
		<link>http://www.ph-lee.com/2009/07/08/how-to-use-ruby-on-rails-activerecord-to-query-multiple-tables-with-joins-and-conditions/</link>
		<comments>http://www.ph-lee.com/2009/07/08/how-to-use-ruby-on-rails-activerecord-to-query-multiple-tables-with-joins-and-conditions/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 15:33:00 +0000</pubDate>
		<dc:creator>ph-lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[multiple]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[table]]></category>

		<guid isPermaLink="false">http://www.ph-lee.com/?p=88</guid>
		<description><![CDATA[This is a back-to-basics article I decided to revisit. Here we&#8217;ll demonstrate how to query two related tables using ActiveRecord with joins and conditions rather than having to write the SQL by hand. Instead we let Activerecord do all the hard work. I like to learn by example so let&#8217;s set the scene.
We&#8217;re an estate [...]]]></description>
			<content:encoded><![CDATA[<p>This is a back-to-basics article I decided to revisit. Here we&#8217;ll demonstrate how to query two related tables using ActiveRecord with joins and conditions rather than having to write the SQL by hand. Instead we let Activerecord do all the hard work. I like to learn by example so let&#8217;s set the scene.</p>
<p>We&#8217;re an estate agent and have an application which stores client details and property details. There are many clients and many properties, a client can have many properties but a property can only belong to one client. A simple one-many relationship. Here are some quick models.</p>
<p>models/client.rb</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Client <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
   <span style="color:#5A0A0A; font-weight:bold;">has_many</span> <span style="color:#ff3333; font-weight:bold;">:properties</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>models/property.rb</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Property <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
   <span style="color:#5A0A0A; font-weight:bold;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:client</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Heres some information the above two tables may contain for the purpose of this example&#8230;</p>
<p>Client table structure</p>
<ul>
<li>id (primary key)</li>
<li>first_name</li>
<li>last_name</li>
</ul>
<p>Property table structure</p>
<ul>
<li>id (primary key)</li>
<li>client_id (foreign key)</li>
<li>house_number</li>
<li>address_first_line</li>
</ul>
<p>#Note that client_id is needed to keep a relationship between the two tables. This foreign key is handled by rails ActiveRecord. Since we specified the relationship in our models it will use client_id to look up primary keys in the client table (ie. client.id).</p>
<p>controllers/home.rb</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> HomeController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
   <span style="color:#9966CC; font-weight:bold;">def</span> my_very_specific_query
      my_client = Client.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:joins</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:properties</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:first_name <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Fred&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:properties</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:house_number <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>The above is very specific query which suits the purpose of this example but rather useless in the real world. It finds the first client in the database (sort by id in ascending order by default) where their first name is &#8220;Fred&#8221; AND owns a property with the house number three.</p>
<p>:joins performs a INNER JOIN, (however if you need you can overide this by writing the exact join by specifying the SQL). This will let you specify conditions for your query which regard related tables.</p>
<p>:conditions lets you specifying the query conditions that will return when true. We have used a hash in this case specifying conditions from the model/table we wish to return (in this case Client) and condition in a related table (Property). Of course extra joins and conditions can be added to suit the need of your query. For further info please see the <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html">API on ActiveRecord</a></p>
<p>You should now be able to use ActiveRecord to query multiple tables using :joins and :conditions</p>
<p>Hope you found this useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ph-lee.com/2009/07/08/how-to-use-ruby-on-rails-activerecord-to-query-multiple-tables-with-joins-and-conditions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How-to : Deploy Ruby on Rails in a subdirectory and not in root</title>
		<link>http://www.ph-lee.com/2009/02/27/deploying-ruby-on-rails-in-a-subdirectory-and-not-in-root/</link>
		<comments>http://www.ph-lee.com/2009/02/27/deploying-ruby-on-rails-in-a-subdirectory-and-not-in-root/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 02:37:11 +0000</pubDate>
		<dc:creator>ph-lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[domain]]></category>
		<category><![CDATA[environment.rb]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[root]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[subdirectory]]></category>

		<guid isPermaLink="false">http://www.ph-lee.com/?p=24</guid>
		<description><![CDATA[Normally one would deploy their rails application in the root directory (top-level) ie where the domain is, for example&#8230;
www.your-domain.com
However, you may want to deploy your app in a subdirectory such as&#8230;
www.your-domain.com/your_app
You may want to do this if you want to deploy several applications on the same domain or temporarily for testing purposes. So heres what [...]]]></description>
			<content:encoded><![CDATA[<p>Normally one would deploy their rails application in the root directory (top-level) ie where the domain is, for example&#8230;</p>
<p><code>www.your-domain.com</code></p>
<p>However, you may want to deploy your app in a subdirectory such as&#8230;</p>
<p><code>www.your-domain.com/your_app</code></p>
<p>You may want to do this if you want to deploy several applications on the same domain or temporarily for testing purposes. So heres what to do. Naviagate to the environment.rb file found here&#8230;</p>
<p><code>root | configuration | environment.rb</code></p>
<p>Open the file in your favourite text editor. Add the relative url configuration to your config as shown below. I added it to the end of the do statement.</p>
<p><code>Rails::Initializer.run do |config|<br />
config.action_controller.relative_url_root = "/your_app"<br />
end</code></p>
<p>This setups the environment for rails to now run with url root of your_app.</p>
<p>Happy deploying!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ph-lee.com/2009/02/27/deploying-ruby-on-rails-in-a-subdirectory-and-not-in-root/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
