<?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>Ahabman &#187; rails</title>
	<atom:link href="http://ahabman.com/blog/category/code/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://ahabman.com/blog</link>
	<description></description>
	<lastBuildDate>Sun, 27 Nov 2011 19:52:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>rails template</title>
		<link>http://ahabman.com/blog/2009/03/rails-template/</link>
		<comments>http://ahabman.com/blog/2009/03/rails-template/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 04:14:08 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.ahabman.com/blog/?p=37</guid>
		<description><![CDATA[Usage $ rails app_name -m ./ahabman_template.rb]]></description>
			<content:encoded><![CDATA[<p>Usage $ rails app_name -m ./ahabman_template.rb</p>
<pre class="brush: ruby; title: ; notranslate">

run &quot;rm README&quot;
run &quot;rm public/index.html&quot;
run &quot;rm public/favicon.ico&quot;
run &quot;rm public/robots.txt&quot;
run &quot;rm -f public/javascripts/*&quot;

gem 'ruby-openid', :lib =&gt; 'openid'
rake(&quot;gems:install&quot;, :sudo =&gt; true)

plugin &quot;kamui_restful_authentication&quot;, :git =&gt; 'git://github.com/kamui/restful-authentication.git'
plugin 'exception_notifier', :git =&gt; 'git://github.com/rails/exception_notification.git'
plugin 'open_id_authentication', :git =&gt; 'git://github.com/rails/open_id_authentication.git'
#plugin 'asset_packager', :git =&gt; 'http://synthesis.sbecker.net/pages/asset_packager'
plugin 'role_requirement', :git =&gt; 'git://github.com/timcharper/role_requirement.git'
#plugin 'acts_as_taggable_redux', :git =&gt; 'http://github.com/geemus/acts_as_taggable_redux/tree/master'
#plugin 'restful-authentication', :git =&gt; 'git://github.com/technoweenie/restful-authentication.git'

generate(&quot;authenticated user sessions --include-activation --include-forgot-password --email-as-login&quot;)
generate(&quot;roles&quot;, &quot;Role User&quot;)

#rake('acts_as_taggable:db:create')
rake(&quot;open_id_authentication:db:create&quot;)
rake('db:migrate')

route &quot;map.root :controller =&gt; 'sessions', :action =&gt; 'signup' &quot;

run &quot;echo generated from ahabman_template &gt; README&quot;
run &quot;cp config/database.yml config/example_database.yml&quot;

#if yes?(&quot;Do you want this thing?&quot;)
# ...
#end

#my_var = ask(&quot;was up&quot;)
#generate :something, my_var

puts &quot;SUCCESS!&quot;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2009/03/rails-template/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ruby on rails &#8211; has and belongs to many view plugin</title>
		<link>http://ahabman.com/blog/2008/10/ruby-on-rails-has-and-belongs-to-many-view-plugin/</link>
		<comments>http://ahabman.com/blog/2008/10/ruby-on-rails-has-and-belongs-to-many-view-plugin/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 12:59:27 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.ahabman.com/blog/?p=35</guid>
		<description><![CDATA[So I&#8217;ll never have to write another has_and_belongs_to_many view again, here&#8217;s a plugin that dynamically makes 2 &#60;ul&#62; lists which you drag and drop both ways to manage the association. http://svn.ahabman.com/public/many_to_many_view/ From the readme: Assumming you have an @project var and you want to associate people with it. "shared/m2m", :locals => { :thing => @project, [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ll never have to write another has_and_belongs_to_many view again, here&#8217;s a plugin that dynamically makes 2 &lt;ul&gt; lists which you drag and drop both ways to manage the association.</p>
<p><a href="http://svn.ahabman.com/public/many_to_many_view/">http://svn.ahabman.com/public/many_to_many_view/</a></p>
<p>From the readme:</p>
<p>Assumming you have an @project var and you want to associate people with it.</p>
<pre>
<div id="m2m_container">
<%= render :partial => "shared/m2m", :locals => {
:thing => @project,
:association => 'people',
:association_attr => 'first_name' } %>
</div>
</pre>
<p>:thing take the current object<br />
:association takes the pluralized association, a string.<br />
:association_attr takes the attribute of the associated object that will be displayed.</p>
<p><a href="http://www.ahabman.com/blog/wp-content/uploads/2008/10/many-to-many-view-plugin.png"><img class="alignnone size-medium wp-image-36" title="many-to-many-view-plugin" src="http://www.ahabman.com/blog/wp-content/uploads/2008/10/many-to-many-view-plugin.png" alt="" width="471" height="253" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2008/10/ruby-on-rails-has-and-belongs-to-many-view-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>You can&#8217;t use class variables in rails.</title>
		<link>http://ahabman.com/blog/2008/10/you-cant-use-class-variables-in-rails/</link>
		<comments>http://ahabman.com/blog/2008/10/you-cant-use-class-variables-in-rails/#comments</comments>
		<pubDate>Wed, 15 Oct 2008 12:51:12 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.ahabman.com/blog/?p=34</guid>
		<description><![CDATA[You can&#8217;t use class variables in rails. Unless you&#8217;d like to develop in production mode (specifically with config.cache_classes = true). In development mode all classes are reloaded on each request, which clears all class variables. I had the perfect place to use them &#8211; while running a mass synchronization, and needing to store the timestamp [...]]]></description>
			<content:encoded><![CDATA[<p>You can&#8217;t use class variables in rails. Unless you&#8217;d like to develop in production mode (specifically with config.cache_classes = true).  In development mode all classes are reloaded on each request, which clears all class variables.</p>
<p>I had the perfect place to use them &#8211; while running a mass synchronization, and needing to store the timestamp (I couldn&#8217;t use the latest last_updated from each record).  It worked perfect in ./script/console but failed entirely through the browser.</p>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2008/10/you-cant-use-class-variables-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ruby csv to structured hash</title>
		<link>http://ahabman.com/blog/2008/09/ruby-csv-to-structured-hash/</link>
		<comments>http://ahabman.com/blog/2008/09/ruby-csv-to-structured-hash/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 20:29:02 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.ahabman.com/blog/?p=31</guid>
		<description><![CDATA[I had a csv like this: object color flavor shape apple red sweet round banana yellow sweet long lemon yellow sour round and I wanted a ruby hash structured like this: So I wrote this, which does the job:]]></description>
			<content:encoded><![CDATA[<p>I had a csv like this:</p>
<table style="height: 84px;" border="1" width="166">
<tbody>
<tr>
<th>object</th>
<th>color</th>
<th>flavor</th>
<th>shape</th>
</tr>
<tr>
<td>apple</td>
<td>red</td>
<td>sweet</td>
<td>round</td>
</tr>
<tr>
<td>banana</td>
<td>yellow</td>
<td>sweet</td>
<td>long</td>
</tr>
<tr>
<td>lemon</td>
<td>yellow</td>
<td>sour</td>
<td>round</td>
</tr>
</tbody>
</table>
<p>and I wanted a ruby hash structured like this:</p>
<pre class="brush: ruby; title: ; notranslate">
{
'apple'=&gt; { 'color'=&gt;'red', 'flavor'=&gt;'sweet', 'shape'=&gt;'round'},
'banana'=&gt; { 'color'=&gt;'yellow', 'flavor'=&gt;'sweet', 'shape'=&gt;'long'},
'lemon'=&gt; { 'color'=&gt;'yellow', 'flavor'=&gt;'sour', 'shape'=&gt;'round'}
}
</pre>
<p>So I wrote this, which does the job:</p>
<pre class="brush: ruby; title: ; notranslate">
require &quot;faster_csv&quot;

def csv_to_structured_hash
	arr_of_arrs = FasterCSV.read( 'your.csv' )
	stuff = {}
	header = arr_of_arrs.shift
		arr_of_arrs.each_with_index do |row, i|
		thing = { row[0] =&gt; {} }
			header.each_with_index do |col, header_index|
			thing[ row[0] ][ header[header_index] ] = row[ header_index]
			end
		stuff.update( thing )
		end
	return stuff
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2008/09/ruby-csv-to-structured-hash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby conversion module</title>
		<link>http://ahabman.com/blog/2008/07/ruby-conversion-module/</link>
		<comments>http://ahabman.com/blog/2008/07/ruby-conversion-module/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 12:52:11 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.ahabman.com/blog/?p=26</guid>
		<description><![CDATA[This is a ruby module useful for conversions dealing with length, weight, torque.]]></description>
			<content:encoded><![CDATA[<p>This is a <a href="http://www.ahabman.com/blog/wp-content/uploads/2008/07/convert.rb">ruby module useful for conversions</a> dealing with length, weight, torque.</p>
<pre class="brush: ruby; title: ; notranslate">
 module Convert 

   def Convert.number_with_precision(number, precision=2)
     &quot;%01.#{precision}f&quot; % number
   rescue
     number
   end 

   def Convert.mm_to_in(mm, precision=2)
     number_with_precision(mm * 0.03937, precision)
   end 

   def Convert.in_to_mm(inches, precision=2)
     number_with_precision(inches / 0.03937 , precision)
   end 

   def Convert.feet_to_meters(f, precision=2)
     number_with_precision( f * 0.3048, precision)
   end 

   def Convert.meters_to_inches(m, precision=2)
     number_with_precision( m * 39.37, precision)
   end 

   def Convert.meters_to_feet(m, precision=2)
     number_with_precision( m * 3.281, precision)
   end 

   def Convert.kg_to_lbs(kg, precision=2)
     number_with_precision( kg * 2.2   , precision)
   end 

   def Convert.lbs_to_kg(lbs, precision=2)
     number_with_precision( lbs / 2.2   , precision)
   end 

   def Convert.nm_to_inch_pounds(nm, precision=1)
     number_with_precision( nm * 8.850   , precision)
   end 

   def Convert.inch_pounds_to_nm(inlb, precision=1)
     number_with_precision( inlb / 8.850   , precision)
   end 

 end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2008/07/ruby-conversion-module/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails redirect_to :back in Internet Explorer</title>
		<link>http://ahabman.com/blog/2008/06/rails-redirect_to-back-in-internet-explorer/</link>
		<comments>http://ahabman.com/blog/2008/06/rails-redirect_to-back-in-internet-explorer/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 21:03:12 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.ahabman.com/blog/?p=25</guid>
		<description><![CDATA[Today I realized that the rails command redirect_to :back does not work in Internet Explorer because redirect_to :back depends on the HTTP_REFERER http header, which IE does not send. I was capturing an onclick event with jQuery and was able to sending along the current URL as a query string, which provided an easy way [...]]]></description>
			<content:encoded><![CDATA[<p>Today I realized that the rails command   redirect_to :back  does not work in Internet Explorer because  redirect_to :back  depends on the HTTP_REFERER http header, which IE does not send.  I was capturing an onclick event with jQuery and was able to sending along the  current URL as a query string, which provided an easy way around this limitation.</p>
<p>In the view:</p>
<pre class="brush: jscript; title: ; notranslate">
&lt;script&gt;
    $jQuery('#myLink').click(function(){
        window.location.href = 'theDestinationPage/?current_url=' + document.location ;
    })
&lt;/script&gt;
</pre>
<p>In the controller:</p>
<pre class="brush: ruby; title: ; notranslate">
def myAction
    .....
    redirect_to params[:current_url]
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2008/06/rails-redirect_to-back-in-internet-explorer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails background process &#8211; simple, fast, easy.</title>
		<link>http://ahabman.com/blog/2008/05/rails-background-process-simple-fast-easy/</link>
		<comments>http://ahabman.com/blog/2008/05/rails-background-process-simple-fast-easy/#comments</comments>
		<pubDate>Sat, 31 May 2008 22:46:12 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.ahabman.com/blog/?p=24</guid>
		<description><![CDATA[My situation was this - Rails 2.0 app running on slicehost (ubuntu 8.04) in passenger mod_rails The goal was Dynamic PDF generation for a product catalog ( &#62; 100 pages) This event had to be triggered from within the rails request / response cycle, but run outside of it I needed the simplest, fastest, more [...]]]></description>
			<content:encoded><![CDATA[<p>My situation was this -</p>
<ul>
<li>Rails 2.0 app running on slicehost (ubuntu 8.04) in <a href="http://www.modrails.com/">passenger mod_rails</a></li>
<li>The goal was <a href="http://ruby-pdf.rubyforge.org/pdf-writer/">Dynamic PDF generation</a> for a product catalog ( &gt; 100 pages)</li>
<li>This event had to be triggered from within the rails request / response cycle, but run outside of it</li>
<li>I needed the simplest, fastest, more reliable way for this to happen</li>
</ul>
<p>I looked at <a href="http://backgroundrb.rubyforge.org/">BackgroundRB</a>, it is the most robust, well documented solution available.  But it was overkill since I didn&#8217;t need status feedback and my time was very limited (backgroundRB setup seems somewhat involved).  And, I wasn&#8217;t sure how it would interact with Passenger mod_rails &#8211; although <a href="http://github.com/FooBarWidget">FooBarWidget</a> mentioned on IRC that it should work as expected.</p>
<p>I looked at <a href="http://hearmesqueak.blogspot.com/2007/09/spawn-background-processing-in-rails.html">Spawn plugin</a> which supports threading and forking ruby processes.   No luck &#8211; setting it one way never ran inside mod_rails and setting it the other way tied up the request response cycle.</p>
<p>All the ruby code was in place, except for how to detach this process from the request/response cycle, when <a href="http://severnspoon.com/">severnspoon</a> provided this<br />
<strong>Simple, Easy, One-Line Solution:</strong></p>
<pre>My_Controller</pre>
<pre>  def generate_pdf_in_background
</pre>
<pre>    system " RAILS_ENV=#{RAILS_ENV}   ruby  #{RAILS_ROOT}/script/runner   'MyModel.create_pdf_in_background'  <a href="http://lynx.astro.ku.dk/comp-phys/tutorials/background.shtml">&amp;</a> "
</pre>
<pre>  end</pre>
<pre>end
</pre>
<p>That ampersand was all I needed.  It runs the command as a background process.  A new ruby process is kicked off, and the 10 minute task runs perfectly.  Something makes the browser hang and wait for a response, but we confirmed that other requests can happen along side this.</p>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2008/05/rails-background-process-simple-fast-easy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mod_rails set RAILS_ENV variable to QA, Staging, or Production</title>
		<link>http://ahabman.com/blog/2008/05/mod_rails-set-rails_env-variable-to-qa-staging-or-production/</link>
		<comments>http://ahabman.com/blog/2008/05/mod_rails-set-rails_env-variable-to-qa-staging-or-production/#comments</comments>
		<pubDate>Sat, 24 May 2008 01:32:14 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[rails mod_rails capistrano]]></category>

		<guid isPermaLink="false">http://www.grandrapidswebdeveloper.com/blog/2008/05/mod_rails-set-rails_env-variable-to-qa-staging-or-production/</guid>
		<description><![CDATA[I&#8217;m using mod_rails with capistrano and multi-stage (qa, staging, production), and wrote this workaround so that the RAILS_ENV could be set correctly in each stage. Mod_rails sets RAILS_ENV once in the global server conf file, but I needed it set once for each environment: qa, staging, and production. I tried setting ENV['RAILS_ENV] in each file [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using mod_rails with capistrano and multi-stage (qa, staging, production), and wrote this workaround so that the RAILS_ENV could be set correctly in each stage.  Mod_rails sets <a href="http://www.modrails.com/documentation/Users%20guide.html#rails_env" target="_blank">RAILS_ENV once in the global server conf file</a>, but I needed it set once for each environment: qa, staging, and production.  I tried setting ENV['RAILS_ENV] in each file under config/deploy/ but the setting was not picked up.</p>
<p>Solution &#8211; write the correct value into config/environment.rb&#8217;s  ENV[''RAILS_ENV] while deploying.</p>
<p>So here is my 3 step fix -</p>
<p>Step 1.</p>
<p>I created a new file</p>
<p>lib/set_mod_rails_env.rb</p>
<pre class="brush: ruby; title: ; notranslate">
# sets ENV['rails_env'] for mod_rails
# &lt;a href=&quot;http://www.megasolutions.net/ruby/search-a-file-and-replace-text-50116.aspx&quot; target=&quot;_blank&quot;&gt;http://www.megasolutions.net/ruby/search-a-file-and-replace-text-50116.aspx&lt;/a&gt;
# Robert Evans'
def ChangeOnFile(file, regex_to_find, text_to_put_in_place)
  text = File.read file
  File.open(file, 'w+'){|f| f &amp;lt;&amp;lt; text.gsub(regex_to_find,
      text_to_put_in_place)}
end

ChangeOnFile(&quot;#{ARGV[0]}/config/environment.rb&quot;, /#mod_rails_env_here/, &quot;ENV['RAILS_ENV']='#{ARGV[1]}'&quot;)
</pre>
<p>Step 2.</p>
<p>Add the following code to config/deploy.rb</p>
<pre class="brush: ruby; title: ; notranslate">
namespace :deploy do
  desc &quot;set ENV['RAILS_ENV'] for mod_rails&quot;
  task :before_restart do
    run &quot;ruby #{current_release}/lib/set_mod_rails_env.rb  #{current_release}  #{stage}&quot;
  end
end
</pre>
<p>Step 3.</p>
<p>Add the following code to config/environment.rb</p>
<pre class="brush: ruby; title: ; notranslate">
# deploy.rb will swap this out for the appropriate rails_env.
# mod_rails apparently need this var in this file (not ./environments/qa.rb)
# do not change the following line. Ruby regexp looks for it.
#mod_rails_env_here
</pre>
<p>That&#8217;s it.  Let me know if you know of an easier way.</p>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2008/05/mod_rails-set-rails_env-variable-to-qa-staging-or-production/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

