<?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; sql</title>
	<atom:link href="http://www.ph-lee.com/tag/sql/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>Tip : Checking SQL generated by Active Record queries</title>
		<link>http://www.ph-lee.com/2009/07/13/tip-checking-sql-generated-by-active-record-queries/</link>
		<comments>http://www.ph-lee.com/2009/07/13/tip-checking-sql-generated-by-active-record-queries/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 13:41:04 +0000</pubDate>
		<dc:creator>ph-lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.ph-lee.com/?p=109</guid>
		<description><![CDATA[Those coming from PHP background or oher languages and have not had experience with object-relational mapping may at first not seem confident with the concept, still prefering to write raw SQL rather than using the tools available.
One tip I can give is to check the SQL queries that Active Record has generated and submitted to [...]]]></description>
			<content:encoded><![CDATA[<p>Those coming from PHP background or oher languages and have not had experience with <a href="http://en.wikipedia.org/wiki/Object-relational_mapping">object-relational mapping</a> may at first not seem confident with the concept, still prefering to write raw SQL rather than using the tools available.</p>
<p>One tip I can give is to check the SQL queries that Active Record has generated and submitted to the databse. This can be done by viewing the console where the server was started (ie the command line where ruby script/server was executed). All SQL submitted to the database are displayed here when running in development mode. You can also check the logs in log/development.log, here all the text from the script/server console are stored for debugging purposes.</p>
<p>Hope this helps users become more confident in using Active Record rather than writing the SQL by hand.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ph-lee.com/2009/07/13/tip-checking-sql-generated-by-active-record-queries/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>
	</channel>
</rss>
