<?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>Tech Trivia &#187; Programming</title>
	<atom:link href="http://www.alethe.com/brad/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alethe.com/brad</link>
	<description>For those who know just enough to be dangerous</description>
	<lastBuildDate>Sun, 28 Feb 2010 04:45:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Multiple select list in rails</title>
		<link>http://www.alethe.com/brad/2009/10/multiple-select-list-in-rails/</link>
		<comments>http://www.alethe.com/brad/2009/10/multiple-select-list-in-rails/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 13:58:21 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby and Rails]]></category>

		<guid isPermaLink="false">http://www.alethe.com/brad/?p=70</guid>
		<description><![CDATA[I have recently been tinkering with ruby on rails again and was banging my head against the wall with a multiple select list for a &#8220;has and belongs to many&#8221; relationship between models. After a lot of Googling and experimenting I finally got it running. The resulting code is deceptively simple! This example illustrates the [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently been tinkering with ruby on rails again and was banging my head against the wall with a multiple select list for a &#8220;has and belongs to many&#8221; relationship between models.  After a lot of Googling and experimenting I finally got it running.  The resulting code is deceptively simple!</p>
<p>This example illustrates the relationship between users and their roles.  Each user can have multiple roles, and each role can have multiple users.</p>
<p><span id="more-70"></span></p>
<h3>The User Model</h3>
<pre class="brush: ruby">
# models/admin/user.rb
class Admin::User &lt; ActiveRecord::Base
  has_one :division, :class_name =&gt; &quot;Admin::Division&quot;
  has_and_belongs_to_many :role, :class_name =&gt; &quot;Admin::Role&quot;
  validates_presence_of :first_name, :last_name, :user_name, :password
end
</pre>
<h3>The Role Model</h3>
<pre class="brush: ruby">
# models/admin/role.rb
class Admin::Role &lt; ActiveRecord::Base
  has_and_belongs_to_many :user, :class_name =&gt; &quot;Admin::User&quot;
  validates_presence_of :role
end
</pre>
<h3>The User View (partial)</h3>
<pre class="brush: ruby">
# views/admin/users/_form.html.erb
&lt;% form_for(@user) do |f| %&gt;
  &lt;%= f.error_messages %&gt;
  &lt;p&gt;
    &lt;%= f.label :first_name %&gt;&lt;br /&gt;
    &lt;%= f.text_field :first_name %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.label :last_name %&gt;&lt;br /&gt;
    &lt;%= f.text_field :last_name %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.label :user_name %&gt;&lt;br /&gt;
    &lt;%= f.text_field :user_name %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.label :password %&gt;&lt;br /&gt;
    &lt;%= f.password_field :password %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.label :role %&gt;&lt;br /&gt;
	&lt;%= collection_select(:admin_user, :role_ids, @roles, :id, :role,
		{ :selected =&gt; @user.role_ids },
		{:multiple =&gt; true, :role =&gt; &#039;admin_user[role_ids][]&#039; }) %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.submit &#039;Save&#039; %&gt;
  &lt;/p&gt;
&lt;% end %&gt;
</pre>
<h3>The User Controller</h3>
<pre class="brush: ruby">
# excerpt from controllers/admin/users_controller.rb
# This is just the update method
#
  def update
    # if the multiple select list is empty,
    # make sure the role_ids array exists
    params[:admin_user][:role_ids] ||= []

    respond_to do |format|
      if @user.update_attributes(params[:admin_user])
        flash[:notice] = &#039;User was successfully updated.&#039;
        format.html { redirect_to(@user) }
        format.xml  { head :ok }
      else
        format.html { render :action =&gt; &quot;edit&quot; }
        format.xml  { render :xml =&gt; @user.errors, :status =&gt; :unprocessable_entity }
      end
    end
  end
</pre>
<h3>Display Comma Delimited Roles</h3>
<p>Once I got the select list working, I wanted a simple way to display a comma delimited list of a user&#8217;s roles.  I messed about with looping over all roles and just displaying the ones that matched the appropriate values, but it turns out there is a much simpler and elegant way to do this.  The following snippet of code leverages the HMABT mapping between the user model and role model to access all the role names associated with a particular user.  It then joins them all together delimiting the values with commas.</p>
<pre class="brush: ruby">
# excerpt from views/admin/users/index.html.erb
#
&lt;%=h user.role.map(&amp;:role).join(&#039;, &#039;) %&gt;
</pre>
<h3>The Admin Name Space</h3>
<p>Please note that my example is complicated a little by the fact that the models, views and controllers are all within a &#8220;/admin&#8221; name space.  Here&#8217;s an excerpt from my routes.rb file which shows the necessary code to make it work.</p>
<pre class="brush: ruby">
# excerpt from config/routes.rb
map.namespace :admin do |admin|
    admin.resources :roles, :users
  end
</pre>
<h3>Additional Resources</h3>
<p>The following resources were quite helpful to me in getting this working:</p>
<ul>
<li><a href="http://railscasts.com/episodes/17-habtm-checkboxes">Railscast: HABTM Checkboxes</a></li>
<li><a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M001625">RubyOnRails API &#8211; collection_select</a></li>
</ul</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alethe.com/brad/2009/10/multiple-select-list-in-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Date format</title>
		<link>http://www.alethe.com/brad/2008/04/date-format/</link>
		<comments>http://www.alethe.com/brad/2008/04/date-format/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 01:57:34 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby and Rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.alethe.com/brad/?p=39</guid>
		<description><![CDATA[Format a date in ruby using the string from time method (strftime). Format options: %a - The abbreviated weekday name (``Sun'') %A - The full weekday name (``Sunday'') %b - The abbreviated month name (``Jan'') %B - The full month name (``January'') %c - The preferred local date and time representation %d - Day of [...]]]></description>
			<content:encoded><![CDATA[<p>Format a date in ruby using the string from time method (strftime).</p>
<pre>
Format options:
  %a - The abbreviated weekday name (``Sun'')
  %A - The  full  weekday  name (``Sunday'')
  %b - The abbreviated month name (``Jan'')
  %B - The  full  month  name (``January'')
  %c - The preferred local date and time representation
  %d - Day of the month (01..31)
  %H - Hour of the day, 24-hour clock (00..23)
  %I - Hour of the day, 12-hour clock (01..12)
  %j - Day of the year (001..366)
  %m - Month of the year (01..12)
  %M - Minute of the hour (00..59)
  %p - Meridian indicator (``AM''  or  ``PM'')
  %S - Second of the minute (00..60)
  %U - Week  number  of the current year,
          starting with the first Sunday as the first
          day of the first week (00..53)
  %W - Week  number  of the current year,
          starting with the first Monday as the first
          day of the first week (00..53)
  %w - Day of the week (Sunday is 0, 0..6)
  %x - Preferred representation for the date alone, no time
  %X - Preferred representation for the time alone, no date
  %y - Year without a century (00..99)
  %Y - Year with century
  %Z - Time zone name
  %% - Literal ``%'' character
</pre>
<pre class="brush: ruby">
   t = Time.now
   t.strftime(&quot;Today is %m/%d/%Y&quot;)   #=&gt; &quot;Today is 04/29/2008&quot;
   t.strftime(&quot;at %I:%M %p&quot;)            #=&gt; &quot;at 10:04 PM&quot;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.alethe.com/brad/2008/04/date-format/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails for RHEL 5</title>
		<link>http://www.alethe.com/brad/2008/04/ruby-on-rails-for-rhel-5/</link>
		<comments>http://www.alethe.com/brad/2008/04/ruby-on-rails-for-rhel-5/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 02:06:49 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ruby and Rails]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[RHEL]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.alethe.com/brad/?p=37</guid>
		<description><![CDATA[Here is the quick and dirty Ruby on Rails setup for Redhat Enterprise Linux v5.0 with MySQL. (2009-12-04 Update: This does not work with RHEL 5.4. The mysql ruby gem requires ruby 1.8.6 and RHEL 5.4 only ships with ruby 1.8.5) Using yum install ruby and mysql. The command below does not list all the [...]]]></description>
			<content:encoded><![CDATA[<p>Here is the quick and dirty Ruby on Rails setup for Redhat Enterprise Linux v5.0 with MySQL.</p>
<p><em>(2009-12-04 Update: This does not work with RHEL 5.4.  The mysql ruby gem requires ruby 1.8.6 and RHEL 5.4 only ships with ruby 1.8.5)</em></p>
<p>Using <code>yum</code> install ruby and mysql.  The command below does not list all the required packages, but due to dependencies, the additional packages will be installed (or should be &#8211; this is all from memory).<br />
<span id="more-37"></span><br />
<code>[bm@app3 ~]$ sudo su -<br />
[root@app3 ~]# yum install ruby ruby-devel ruby-libs ruby-irb \<br />
> ruby-rdoc subversion-ruby mysql-server mysql-devel<br />
[....]</code></p>
<p>Complete the basic configuration of MySQL.<br />
<code>[root@app3 ~]# /sbin/chkconfig mysqld on<br />
[root@app3 ~]# service mysqld start<br />
[root@app3 ~]# mysql_secure_installation<br />
</code></p>
<p><a href="http://rubyforge.org/projects/rubygems/">Download</a> and install RubyGems.  As of the date this post was written, the latest version is 1.1.1.<br />
<code>[root@app3 ~]# wget http://rubyforge.org/frs/download.php/35284/rubygems-1.1.1.zip<br />
[root@app3 ~]# unzip rubygems-1.1.1.zip<br />
[root@app3 ~]# cd rubygems-1.1.1<br />
[root@app3 rubygems-1.1.1]# ruby setup.rb<br />
[root@app3 ~]# cd ..<br />
[root@app3 ~]# rm -rf rubygems-1.1.1<br />
</code></p>
<p>Now install Rails, Mongrel, the Ruby MySQL interface, Capistrano<br />
<code>[root@app3 ~]# gem install rails --include-dependencies --no-rdoc --no-ri<br />
[root@app3 ~]# gem install mongrel --include-dependencies --no-rdoc --no-ri<br />
[root@app3 ~]# gem install mysql -- --with-mysql-config=/usr/bin/mysql_config<br />
[root@app3 ~]# gem install capistrano --include-dependencies --no-rdoc --no-ri<br />
</code></p>
<p>Phusion&#8217;s <a href="http://modrails.com/">passenger</a> gem provides a &#8220;mod_rails&#8221; module for Apache.  The installation is very straight forward.<br />
<code>[root@app3 ~]# gem install passenger<br />
[root@app3 ~]# passenger-install-apache2-module<br />
</code></p>
<p>Just follow the on-screen instructions to finish the installation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alethe.com/brad/2008/04/ruby-on-rails-for-rhel-5/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Reduce database connections using :include</title>
		<link>http://www.alethe.com/brad/2008/04/reduce-database-connections-using-include/</link>
		<comments>http://www.alethe.com/brad/2008/04/reduce-database-connections-using-include/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 01:41:19 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby and Rails]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.alethe.com/brad/?p=36</guid>
		<description><![CDATA[When displaying a list that requires information from several tables in the database, you can sometimes end up with three or more database queries per list row. Depending on the situation, this may not be a problem. However, it is possible to join the tables in one large SQL query using the :include option. # [...]]]></description>
			<content:encoded><![CDATA[<p>When displaying a list that requires information from several tables in the database, you can sometimes end up with three or more database queries per list row.  Depending on the situation, this may not be a problem.  However, it is possible to join the tables in one large SQL query using the <code>:include</code> option.</p>
<pre class="brush: ruby">
# :include example
#
@requests = Request.find(:all, :include =&gt; [:department, :status])
</pre>
<p>Using the <code>:include</code> option may improve performance.  However, it will likely use more server memory and could potentially return a lot of data through the joins that may not even be used.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alethe.com/brad/2008/04/reduce-database-connections-using-include/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install phpMyAdmin</title>
		<link>http://www.alethe.com/brad/2008/02/install-phpmyadmin/</link>
		<comments>http://www.alethe.com/brad/2008/02/install-phpmyadmin/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 03:01:13 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.alethe.com/brad/?p=38</guid>
		<description><![CDATA[phpMyAdmin is web-based PHP application for managing MySQL databases. It is fairly easy to install and configure. Download the latest stable version from the phpMyAdmin web site, and extract it to a location of your choice. I prefer to keep the installation outside of the web root and use an apache alias to reference it. [...]]]></description>
			<content:encoded><![CDATA[<p>phpMyAdmin is web-based PHP application for managing MySQL databases.  It is fairly easy to install and configure.</p>
<p>Download the latest stable version from the <a href="http://www.phpmyadmin.net">phpMyAdmin web site</a>, and extract it to a location of your choice.  I prefer to keep the installation outside of the web root and use an apache alias to reference it.<br />
<span id="more-38"></span><br />
<code>[root@app3 ~]# cd /var/www<br />
[root@app3 ~]# wget http://superb-west.dl.sourceforge.net/sourceforge/phpmyadmin/phpMyAdmin-2.11.5.2-all-languages-utf-8-only.tar.gz<br />
[root@app3 ~]# tar xzf phpMyAdmin-2.11.5.2-all-languages-utf-8-only.tar.gz<br />
</code><br />
Now that&#8217;s a lengthy directory name!  Let&#8217;s create a link to that directory with a more friendly name.<br />
<code><br />
[root@app3 ~]# ln -s phpMyAdmin-2.11.5.2-all-languages-utf-8-only phpMyAdmin<br />
</code></p>
<p>SELinux may generate a number of errors regarding this new directory that will be referenced by the apache web server.  Let&#8217;s correct the security context of the entire phpMyAdmin directory structure.<br />
<code><br />
[root@app3 ~]# chcon -R -h -t httpd_sys_content_t \<br />
> phpMyAdmin-2.11.5.2-all-languages-utf-8-only<br />
</code></p>
<p>Configure the phpMyAdmin installation to use http authentication.  We&#8217;ll make sure it is secure by forcing SSL encryption using apache directives in a later step.<br />
<code><br />
[root@app3 ~]# cd phpMyAdmin<br />
[root@app3 ~]# cp config.sample.inc.php config.inc.php<br />
[root@app3 ~]# vi config.inc.php<br />
</code></p>
<p>Change the line:<br />
<code>$cfg['Servers'][$i]['auth_type'] = 'cookie';</code><br />
to:<br />
<code>$cfg['Servers'][$i]['auth_type'] = 'http';</code></p>
<p>Now we need to configure the apache web server.  To keep things modular, create a file <code>phpmyadmin.conf</code> in <code>/etc/httpd/conf.d</code>.  Any configuration directives in this file will automatically be loaded into the main apache configuration when the daemon is restarted.<br />
<code><br />
[root@app3 ~]# vi /etc/httpd/conf.d/phpmyadmin.conf<br />
</code></p>
<p>The <code>phpmyadmin.conf</code> file should contain the following directives.  Just modify the paths to suit your installation.<br />
<code></p>
<pre>
Alias /phpmyadmin /var/www/phpMyAdmin
&lt;Directory "/var/www/phpMyAdmin"&gt;
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    SSLRequireSSL
&lt;/Directory&gt;
</pre>
<p></code></p>
<p>Restart apache and see if your installation works.<br />
<code><br />
[root@app3 ~]# service httpd restart<br />
</code></p>
<p>If there are problems, check your logs.  It is possible that SELinux may be complaining about something.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alethe.com/brad/2008/02/install-phpmyadmin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
