<?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>Anomalous &#187; Ruby on Rails</title>
	<atom:link href="http://ryanabbott.com/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://ryanabbott.com</link>
	<description>If things seem a bit odd, thats probably because.. they are.</description>
	<lastBuildDate>Fri, 11 Feb 2011 15:52:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0-beta2-14519</generator>
		<item>
		<title>Using Two Layouts in a Single Rails Controller</title>
		<link>http://ryanabbott.com/2011/01/using-two-layouts-in-a-single-rails-controller/</link>
		<comments>http://ryanabbott.com/2011/01/using-two-layouts-in-a-single-rails-controller/#comments</comments>
		<pubDate>Fri, 28 Jan 2011 15:11:38 +0000</pubDate>
		<dc:creator>Rabbott</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://ryanabbott.com/?p=69</guid>
		<description><![CDATA[Sometimes, whether it is best or not, you run into the issue of having a single controller needing to display two different layouts. I&#8217;ve only run into this a hand full of times but it was annoying each time because &#8230; <a href="http://ryanabbott.com/2011/01/using-two-layouts-in-a-single-rails-controller/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sometimes, whether it is best or not, you run into the issue of having a single controller needing to display two different layouts. I&#8217;ve only run into this a hand full of times but it was annoying each time because Rails doesn&#8217;t handle it by default. Here is my solution:</p>
<script src="http://gist.github.com/800360.js"></script>
]]></content:encoded>
			<wfw:commentRss>http://ryanabbott.com/2011/01/using-two-layouts-in-a-single-rails-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Storing Uploaded Files on Server Filesystem with Rails</title>
		<link>http://ryanabbott.com/2010/08/storing-uploaded-files-on-server-filesystem-with-rails/</link>
		<comments>http://ryanabbott.com/2010/08/storing-uploaded-files-on-server-filesystem-with-rails/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 03:57:59 +0000</pubDate>
		<dc:creator>Rabbott</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://ryanabbott.com/?p=64</guid>
		<description><![CDATA[Storing files in large BLOB columns of your database has its place, but in situations in which that is not the case, one needs a well organized way to keep files on the server. Often times folks will create a &#8230; <a href="http://ryanabbott.com/2010/08/storing-uploaded-files-on-server-filesystem-with-rails/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Storing files in large BLOB columns of your database has its place, but in situations in which that is not the case, one needs a well organized way to keep files on the server. Often times folks will create a folder called &#8220;uploaded_files&#8221; and dump all of their files into that directory.. which works until you get more than 200 files in that directory &#8211; then what!? The directory cannot be copied and cannot be opened remotely because of its size, and even if it can be, how do you find a file if need be &#8212; you can&#8217;t!</p>
<p>Tonight I put together a very basic upload project to display how I sometimes accomplish the file upload and storage.</p>
<p><a href="http://github.com/loudpixel/simple-rails-file-uploads">http://github.com/loudpixel/simple-rails-file-uploads</a> will show you how to manage files on your server in a well organized way. This method enables you to define a base directory where the files will be stored (e.g. /public/uploaded_files &#8211; just like before) but when it saves files to the file system it organizes them in folders in the format of YYYY/MM/DD/ based on the created_at value of the files.</p>
<p>Do you store files a different way, have questions as to why I use this method?</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanabbott.com/2010/08/storing-uploaded-files-on-server-filesystem-with-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Removing Non-alphanumeric Characters Using REGEX</title>
		<link>http://ryanabbott.com/2010/08/removing-non-alphanumeric-characters-using-regex/</link>
		<comments>http://ryanabbott.com/2010/08/removing-non-alphanumeric-characters-using-regex/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 03:09:14 +0000</pubDate>
		<dc:creator>Rabbott</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://ryanabbott.com/?p=59</guid>
		<description><![CDATA[We use this fancy (not really) method at Loudpixel that allows us to create dynamic stylesheets (not using a framework, just creating application.css.erb) in order to apply certain styles (mainly colors and images) to specific areas of our layouts. In &#8230; <a href="http://ryanabbott.com/2010/08/removing-non-alphanumeric-characters-using-regex/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>We use this fancy (not really) method at <a href="http://loudpixel.com">Loudpixel</a> that allows us to create dynamic stylesheets (not using a framework, just creating application.css.erb) in order to apply certain styles (mainly colors and images) to specific areas of our layouts. In order to do this we want to use a company&#8217;s name as a css rule.</p>
<p>When I initially implemented this I just down-cased the company name, which worked for the brand I was testing with so I moved on. Shortly thereafter I noticed some errors showing up in our dynamic stylesheet, why, well because not every company&#8217;s name is one word, with no apostrophes or any other foreign characters!</p>
<p>So in order to solve this issue, I slapped in a simple REGEX that removes all non-alphanumeric characters from the company&#8217;s name AND downcases it &#8211; perfect for css rules!</p>
<script src="http://gist.github.com/533212.js"></script>
<p>This really has many uses when you consider file names, URLs, CSS rules, and markup classes and ID&#8217;s.</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanabbott.com/2010/08/removing-non-alphanumeric-characters-using-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Metadata in Ruby on Rails</title>
		<link>http://ryanabbott.com/2010/05/metadata-in-ruby-on-rails/</link>
		<comments>http://ryanabbott.com/2010/05/metadata-in-ruby-on-rails/#comments</comments>
		<pubDate>Thu, 13 May 2010 12:24:02 +0000</pubDate>
		<dc:creator>Rabbott</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://ryanabbott.com/?p=3</guid>
		<description><![CDATA[I have multiple pieces of data to store that all relate to one row in my database, but I don&#8217;t want to create a completely separate data model and/or table. Is it possible to store this data in a single &#8230; <a href="http://ryanabbott.com/2010/05/metadata-in-ruby-on-rails/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>I have multiple pieces of data to store that all relate to one row in my database, but I don&#8217;t want to create a completely separate data model and/or table. Is it possible to store this data in a single column within the same database row?</p></blockquote>
<p>I recently had this exact problem, I had a single row in my database that referred to one or many (I am using Rails STI, the amount of data depends on the objects stored within the row) other pieces of arbitrary data. This is most commonly referred to as &#8216;metadata&#8217; and would typically have its own table of key value pairs and a foreign key; but in my case I wont need it that often, and didn&#8217;t want to create another table to handle such small amounts of data. So I thought &#8220;there is no way that someone else hasn&#8217;t faced this issue&#8221;, and naturally turned to Google.</p>
<p>I was surprised at how little I found; what I did find were different discussions about creating metadata for plugins, or even storing JSON in the column (not bad, but how do you search for data in JSON from MySQL?); nothing too helpful, leaving me to fend for myself.</p>
<p>After days of racking my brain I had a thought; XML has standards, it has practical uses, and it&#8217;s widely used and understood, so why not store the information I need as a mini XML document? This would allow me to store as much information as I want to, in one column &#8211; and using methods within MySQL I can even search the data. Below I will walk you through a simple Rails solution demonstrating how to use this metadata method, built on top of simple Single Table Inheritance (STI).</p>
<p>This example will use vehicles (cars and trucks) as our STI model, and our metadata will store information about each cars specifications.</p>
<p><strong>Basics of our STI classes, with metadata column</strong><br />
<script src="http://gist.github.com/395367.js"></script></p>
<p><strong>Writing to our metadata column</strong><br />
In order for the setter to write correctly, it expects a hash. The data submitted from a form (POST data) is already in the form of a hash, but the reason for implementing this method was to allow for more than one value to be stored here, this requires a little work on our end. Lets say we want to store information about the car that will go on the sticker within a dealership.. We never know what could end up on that sticker, but we have one column to store it.</p>
<p>In order to control what happens to the data within this field, we will need to write a custom setter for the metadata attribute. The setter method that we create will allow us to handle the incoming data in any way we want. The method we need to add to our Vehicle class would look like this<br />
<script src="http://gist.github.com/395362.js"></script></p>
<p>This setter expects a hash from our form, this is because of the <a href="http://api.rubyonrails.org/classes/ActiveRecord/Serialization.html#M001876">method</a> we use to convert the data to XML. Since we are providing the hash to the setter we will need to name our form fields accordingly, this can be accomplished like so<br />
<script src="http://gist.github.com/395369.js"></script></p>
<p>When this form is submitted, the following will be written to the database<br />
<script src="http://gist.github.com/395372.js"></script></p>
<p>As you can see, it looks like a mini XML document, header and all. The beauty of it, Rails has built in functionality to read this data, and SQL has built in methods to search this data!</p>
<p><strong>Reading our metadata column</strong><br />
Now, rails supports all the data types that your database supports as far as retrieving and displaying data, and the default getter methods work perfect for those &#8211; unfortunately XML is not one of them. In order to get the data out of our column, in a form that is easily readable by rails, we will add the following custom getter method to our Vehicle class<br />
<script src="http://gist.github.com/395366.js"></script></p>
<p>This getter will retrieve the XML in the column, and return it as a Ruby hash, resembling<br />
<script src="http://gist.github.com/395381.js"></script></p>
<p>Data is this form should be quite familiar, the Hash object is widely used within Ruby and Rails applications. And just like any other Hash, when we want to display this information, we could use something as simple as<br />
<script src="http://gist.github.com/395408.js"></script></p>
<p><strong>Searching this data using SQL</strong><br />
An important aspect of any data, is that it is searchable. Unlike JSON, XML can be searched within a database column using XPATH. Lets say, for instance, that we want to find all cars that are under $10,000.. We can use the following to accomplish that<br />
<script src="http://gist.github.com/395421.js"></script></p>
<p>So there you have it, a relatively easy, clean, scalable way to store arbitrary data associated with a single database row in your Rails applications. Please understand that this is a basic example, if you were to use such code you would want to make sure to take care of the appropriate error checking, and testing.</p>
<p>If you would like to view this post as a working example, I posted a <a href="http://github.com/abbottry/rails-metadata">sample app on github</a>.</p>
<p>I&#8217;m sure there are many ways of accomplishing this, this method worked for me.. Do you have a better method? Suggestions as to how I can improve this one?</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanabbott.com/2010/05/metadata-in-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

