<?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>modsognir</title>
	<atom:link href="http://blog.modsognir.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.modsognir.com</link>
	<description>notes on ruby on rails</description>
	<lastBuildDate>Sat, 26 Sep 2009 14:33:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Searching a Database in Rails</title>
		<link>http://blog.modsognir.com/?p=7</link>
		<comments>http://blog.modsognir.com/?p=7#comments</comments>
		<pubDate>Sat, 26 Sep 2009 14:33:36 +0000</pubDate>
		<dc:creator>XanatosR</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.modsognir.com/?p=7</guid>
		<description><![CDATA[For searching your database, ActiveRecord (AR) provides many methods &#8211; some generated automatically based on your tables. The most basic way to find an object is with calling the class method &#8216;find&#8217; on your model. find, by default, takes the id(s) of the record you&#8217;re looking for as an argument.
User.find(1)
User.find(1, 7, 14)

You can find the [...]]]></description>
			<content:encoded><![CDATA[<p>For searching your database, ActiveRecord (AR) provides many methods &#8211; some generated automatically based on your tables. The most basic way to find an object is with calling the class method &#8216;find&#8217; on your model. find, by default, takes the id(s) of the record you&#8217;re looking for as an argument.</p>
<pre><code>User.find(1)
User.find(1, 7, 14)
</code></pre>
<p>You can find the first, last, and all records by passing a symbol as the first argument. There are also shortcut methods for accessing these records.</p>
<pre><code>User.find(:all)         User.all
User.find(:first)       User.first
User.find(:last)        User.last
</code></pre>
<p>Rails automatically generates a number of dynamic find methods based on your table&#8217;s design. This makes it easy to find records quickly.</p>
<pre><code>User.find_by_email('jared@modsognir.com')
</code></pre>
<p>You can find first, last, and all by adding the word after &#8216;find&#8217; in the chain.</p>
<pre><code>Article.find_all_by_user_id(1)
Article.find_first_by_email('j@modsognir.com')
</code></pre>
<p>And it is easy to find based on multiple columns too.</p>
<pre><code>User.find_first_by_user_name_and_password('Jared', password)
</code></pre>
<p>This covers simple cases. As you delve further into your project, you&#8217;ll likely require more complicated searches. Passing a hash as the last argument allows you to pass more refined information into the query.</p>
<p>The &#8216;:conditions&#8217; option has two modes. The first, by passing a hash, uses AR to form your database query. This abstracts the SQL to Rails, and ensures database compatability as well as protecting from SQL injection attacks.</p>
<pre><code>User.find(:first, :conditions => { :username => 'Jared', :password => password })
</code></pre>
<p>The second is accessed either by passing an Array or a String. You should use this only if you are familiar with SQL syntax and need more specialised cases. . These two queries are identical. </p>
<pre><code>User.find(:first, :conditions => "username LIKE #{user.username}")
User.find(:first, :conditions => ["username LIKE ?", user.username])
</code></pre>
<p>The Array form will also protect you from SQL injection attacks (while the string wont). It works by having a string as the first element containing SQL, then any question mark in that string will be replaced one by one by the remaining elements.</p>
<pre><code>User.find(:first, :conditions => ["username LIKE ? AND email LIKE ?", user.username, user.email])
</code></pre>
<p>When you need to have the returned records ordered in a particular way, use the &#8216;:order&#8217; option. This takes a string as basic SQL.</p>
<pre><code>User.find(:all, <img src='http://blog.modsognir.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder => "created_at DESC")
User.find(:all, :conditions =>; { :role => "admin" }, <img src='http://blog.modsognir.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder => "username ASC")
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.modsognir.com/?feed=rss2&amp;p=7</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Rails Migration Data Types</title>
		<link>http://blog.modsognir.com/?p=5</link>
		<comments>http://blog.modsognir.com/?p=5#comments</comments>
		<pubDate>Fri, 29 May 2009 15:50:07 +0000</pubDate>
		<dc:creator>XanatosR</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails migrations mysql]]></category>

		<guid isPermaLink="false">http://blog.modsognir.com/?p=5</guid>
		<description><![CDATA[Had someone ask where to find the available data types for Rails migrations today &#8211; and thought it would be good to keep a copy for my own reference. Heres the list:
Rails -> MySQL
:binary -> blob
:boolean -> tinyint(1)
:date -> date
:datetime ->datetime
:decimal -> decimal
:float ->float
:integer -> int(11)
:string -> varchar(255)
:text -> text
:timestamp -> datetime
:time -> time
]]></description>
			<content:encoded><![CDATA[<p>Had someone ask where to find the available data types for Rails migrations today &#8211; and thought it would be good to keep a copy for my own reference. Heres the list:</p>
<p>Rails -> MySQL<br />
:binary -> blob<br />
:boolean -> tinyint(1)<br />
:date -> date<br />
:datetime ->datetime<br />
:decimal -> decimal<br />
:float ->float<br />
:integer -> int(11)<br />
:string -> varchar(255)<br />
:text -> text<br />
:timestamp -> datetime<br />
:time -> time</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.modsognir.com/?feed=rss2&amp;p=5</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
