<?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; database</title>
	<atom:link href="http://www.ph-lee.com/tag/database/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>Installing mysql on Mac Snow Leopard</title>
		<link>http://www.ph-lee.com/2009/09/18/installing-mysql-on-mac-snow-leopard/</link>
		<comments>http://www.ph-lee.com/2009/09/18/installing-mysql-on-mac-snow-leopard/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 17:11:59 +0000</pubDate>
		<dc:creator>ph-lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database.yml]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[snow leopard]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.ph-lee.com/?p=197</guid>
		<description><![CDATA[This articles is regarding Mac OS X 10.6 with mysql 5.1.37.
I followed this article at hivelogic.com which required compiling mysql from source. It mostly worked with a few hitches which I&#8217;d like to share.
After following the article and attempting to run my rails app the first problem I hit was&#8230;
uninitialized constant MysqlCompat::MysqlRes
this problem seemed to [...]]]></description>
			<content:encoded><![CDATA[<p>This articles is regarding Mac OS X 10.6 with mysql 5.1.37.</p>
<p>I followed <a href="http://hivelogic.com/articles/compiling-mysql-on-snow-leopard/">this article at hivelogic.com</a> which required compiling mysql from source. It mostly worked with a few hitches which I&#8217;d like to share.</p>
<p>After following the article and attempting to run my rails app the first problem I hit was&#8230;</p>
<p><code>uninitialized constant MysqlCompat::MysqlRes</code></p>
<p>this problem seemed to be that you need to specify the atchitecture as 64-bit when installing the mysql gem. So run the below command intead when install the mysql gem&#8230;</p>
<p><code>sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-<br />
config=/usr/local/mysql/bin/mysql_config</code></p>
<p>Or if you already installed the gem just re-install with this command. The next problem was the following&#8230;</p>
<p><code>Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)</code></p>
<p>I&#8217;d move a project from ubuntu onto the mac I couldn&#8217;t find the mysql socket to use as it was specifying the wrong location. On the mac the file can be found at</p>
<p><code>/tmp/mysql.sock</code></p>
<p>So you&#8217;ll need to modify your database.yml file accordingly to point to this location</p>
<p>Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ph-lee.com/2009/09/18/installing-mysql-on-mac-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Gotcha : Mysql::Error: Unknown database &#039;application_name_test&#039;  /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize&#039;</title>
		<link>http://www.ph-lee.com/2009/07/19/gotcha-mysqlerror-unknown-database-application_name_test-usrlibruby18monitorrb242in-synchronize/</link>
		<comments>http://www.ph-lee.com/2009/07/19/gotcha-mysqlerror-unknown-database-application_name_test-usrlibruby18monitorrb242in-synchronize/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 18:03:11 +0000</pubDate>
		<dc:creator>ph-lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.ph-lee.com/?p=146</guid>
		<description><![CDATA[After dealing with thisGotcha : Mysql::Error: Access denied for user ‘root’@&#8217;localhost’ you&#8217;ve hit another problem of&#8230;
This means rails is looking for the test database but can&#8217;t find it cause it&#8217;s not there. Simply got to mysql Query Browser or command line, whichever you prefer and create a new database/schema of your application_name followed by _test [...]]]></description>
			<content:encoded><![CDATA[<p>After dealing with this<a href="http://www.ph-lee.com/2009/07/19/gotcha-mysqlerror-access-denied-for-user-rootlocalhost-using-password-no-usrlibruby18monitorrb242in-synchronize/">Gotcha : Mysql::Error: Access denied for user ‘root’@&#8217;localhost’</a> you&#8217;ve hit another problem of&#8230;</p>
<script src="http://gist.github.com/149984.js"></script>
<p>This means rails is looking for the test database but can&#8217;t find it cause it&#8217;s not there. Simply got to mysql Query Browser or command line, whichever you prefer and create a new database/schema of your application_name followed by _test so for example</p>
<p><code>my_app_test</code></p>
<p>Theres no need to copy/clone the database structure a across. Rails will replicate the development database as the test database.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ph-lee.com/2009/07/19/gotcha-mysqlerror-unknown-database-application_name_test-usrlibruby18monitorrb242in-synchronize/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
