<?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>David Lowry &#187; learning</title>
	<atom:link href="http://www.davidlowry.co.uk/category/learning/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davidlowry.co.uk</link>
	<description></description>
	<lastBuildDate>Sat, 05 Nov 2011 14:35:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ActiveAdmin on Heroku (Rails 3.1)</title>
		<link>http://www.davidlowry.co.uk/400/activeadmin-on-heroku-rails-3-1/</link>
		<comments>http://www.davidlowry.co.uk/400/activeadmin-on-heroku-rails-3-1/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 09:00:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.davidlowry.co.uk/?p=400</guid>
		<description><![CDATA[Just a brief note on getting the new ActiveAdmin gem working with Heroku under Rails 3.1 (on Thin and probably some other servers too) ActiveAdmin looks like a really useful tool to avoid Scaffold-itis when it comes to admin interfacing in Rails apps. You install the gem and loosely define what models you&#8217;re using; and you...<a href="http://www.davidlowry.co.uk/400/activeadmin-on-heroku-rails-3-1/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Just a brief note on getting the new <a href="http://activeadmin.info/">ActiveAdmin</a> gem working with Heroku under Rails 3.1 (on Thin and probably some other servers too)</p>
<p>ActiveAdmin looks like a really useful tool to avoid Scaffold-itis when it comes to admin interfacing in Rails apps. You <a href="http://activeadmin.info/documentation.html">install the gem and loosely define</a> what models you&#8217;re using; and you can create simple effective dashboards.</p>
<p>It works easily on local testing, but when you deploy to production on Heroku (or locally with thin <code>$ bundle exec rails server thin -e production</code> the build will fail citing <code>/Users/dave/.rvm/gems/ruby-1.9.2-p290/gems/sass-rails-3.1.0/lib/sass/rails/railtie.rb:38:in `block in ': uninitialized constant Sass::Rails::SassTemplate (NameError)</code>&#8230; or similar.</p>
<p>By default the SASS gem is defined within &#8216;assets&#8217; in the Gemfile.  Move the line</p>
<p><code><br />
group :assets do<br />
gem 'sass-rails', " ~&gt; 3.1.0"<br />
..<br />
end<br />
</code></p>
<p>outside the assets group, and it will spin up without a problem. <a href="https://github.com/rails/sass-rails/issues/38">It seems the assets group</a> is processed after other gems.</p>
<p>Solution found via this post: <a href="https://github.com/rails/sass-rails/issues/38">https://github.com/rails/sass-rails/issues/38</a><br />
Great <a href="http://railscasts.com/episodes/284-active-admin">introductory ActiveAdmin tutorial at Railscasts</a></p>
<p>Now to actually build the dashboard&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidlowry.co.uk/400/activeadmin-on-heroku-rails-3-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails: Get some random records</title>
		<link>http://www.davidlowry.co.uk/293/ruby-on-rails-get-some-random-records/</link>
		<comments>http://www.davidlowry.co.uk/293/ruby-on-rails-get-some-random-records/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 13:04:38 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://davidlowry.co.uk/?p=293</guid>
		<description><![CDATA[EDIT AGAIN: Much better.. To get a &#8216;num_reqd&#8217; array of random objects, you can use something like this. named_scope :large, :conditions => ['image_file_name IS NOT ?', nil] named_scope :small, :conditions => ['small_image_file_name IS NOT ?', nil] def self.get(num_reqd,features_arr=[],size="large") if size=="small" collection = Feature.small elsif size=="large" collection = Feature.large end return collection if collection.size num_reqd, rder...<a href="http://www.davidlowry.co.uk/293/ruby-on-rails-get-some-random-records/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p>EDIT AGAIN: Much better..</p>
<p>To get a &#8216;num_reqd&#8217; array of random objects, you can use something like this.</p>
<pre><code>  named_scope :large, :conditions => ['image_file_name IS NOT ?', nil]
  named_scope :small, :conditions => ['small_image_file_name IS NOT ?', nil] 

  def self.get(num_reqd,features_arr=[],size="large")
    if size=="small"
      collection = Feature.small
    elsif size=="large"
      collection = Feature.large
    end

    return collection if collection.size <= num_reqd

    # num_reqd.times{feature=self.random(collection); features_arr.push(feature) unless features_arr.include?(feature)}
    features_arr = collection.find(:all, :limit => num_reqd, <img src='http://www.davidlowry.co.uk/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder => 'rand()')

    if features_arr.size < num_reqd
      return Feature.get(num_reqd, features_arr, size)
    else
      return features_arr
    end
  end</code></pre>
<p>EDIT: It's much cleaner and easier to use something in the form below, though the following is probably useful in some cases and is possibly interesting as a code snippet.<br />
<code>
<pre>
User.find(:all, <img src='http://www.davidlowry.co.uk/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder => 'rand()')
</pre>
<p></code><br />
---- end edit.</p>
<p>Working from a baseline of the code found <a href="http://almosteffortless.com/2007/12/04/random-records-in-rails/">here at almosteffortless.com</a> I've extended a 'random record grabber' to get a specific number of unique records from a Rails data table.</p>
<p>Basically - the random method makes a database call to get the ids of a table, and sends back a random entry. <code>self.get</code> is a recursive method which provides a 'total number required' and a base array to start from (if you wish to specify entries to appear in the otherwise 'random' list). First year computer science should help get your head around the rest!</p>
<p><code>
<pre>
def self.random
    ids = connection.select_all("SELECT id FROM features")
    find(ids[rand(ids.length)]["id"].to_i) unless ids.blank?
  end

  def self.get(num_reqd,features_arr=[])
    num_reqd.times{feature=self.random; features_arr.push(feature) unless features_arr.include?(feature)}

    if features_arr.size < num_reqd
      return Feature.get(num_reqd, features_arr)
    else
      return features_arr
    end
  end</pre>
<p></code></p>
<p>Be aware, there is more efficiency to be found in the database call (i.e. it should be cached). Also, you'll want to be sure there are at least 'num_reqd' items in the database.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidlowry.co.uk/293/ruby-on-rails-get-some-random-records/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Thinking Sphinx on Dreamhost</title>
		<link>http://www.davidlowry.co.uk/279/simple-thinking-sphinx-on-dreamhost/</link>
		<comments>http://www.davidlowry.co.uk/279/simple-thinking-sphinx-on-dreamhost/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 13:10:16 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[learning]]></category>
		<category><![CDATA[meaningful labor]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[dreamhost]]></category>
		<category><![CDATA[railscasts]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[sphinx]]></category>
		<category><![CDATA[thinking sphinx]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://davidlowry.co.uk/?p=279</guid>
		<description><![CDATA[*** Please note &#8211; this will probably not work (at all) (for more than a day of light use) without Cron use. And isn&#8217;t at all authorised by Dreamhost!! *** For a recent client project I&#8217;ve used a Dreamhost unlimited account, which for value compared with the resources available and the fact that you don&#8217;t...<a href="http://www.davidlowry.co.uk/279/simple-thinking-sphinx-on-dreamhost/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p>*** Please note &#8211; this will probably not work (at all) (for more than a day of light use) without Cron use. And isn&#8217;t at all authorised by Dreamhost!! *** </p>
<p>For a recent client project I&#8217;ve used a Dreamhost unlimited account, which for value compared with the resources available and the fact that you don&#8217;t have to do any building or setting up of the server environment makes it an easy win for a site that&#8217;s not going to have a huge amount of traffic or a large amount of processing.</p>
<p>Post-launch I got to work putting together a basic search engine and here&#8217;s a quick run through of the steps it took to get a very simple Sphinx instance working on Dreamhost, and a few hurdles thrown in the way by various googled articles.</p>
<h3>Development Environment</h3>
<p>Using the <a href="http://freelancing-gods.com/posts/a_concise_guide_to_using_thinking_sphinx">guide from FG</a> install Sphinx locally:</p>
<pre><code>curl -O http://sphinxsearch.com/downloads/sphinx-0.9.8-rc2.tar.gz
tar zxvf sphinx-0.9.8-rc2.tar.gz
cd sphinx-0.9.8-rc2
./configure
make
sudo make install</code></pre>
<p>then install the TS plugin into your application</p>
<pre><code>script/plugin install git://github.com/freelancing-god/thinking-sphinx.git
</code></pre>
<p>Any problems with that, check out the <a href="http://freelancing-gods.com/posts/a_concise_guide_to_using_thinking_sphinx">FG page</a> linked.</p>
<h3>Getting a basic search going</h3>
<p>Following tutorials such as the <a href="http://railscasts.com/episodes/120-thinking-sphinx">Sphinx Railscast</a> will get you there pretty quick.</p>
<p>In your searchable model you need to define an index</p>
<pre><code>
class Page < ActiveRecord::Base
  define_index do
    indexes :title
    indexes :long
    indexes :short
  end ...</code></pre>
<p>Run the indexer and start the Sphinx instance:</p>
<pre><code>
rake thinking_sphinx:index
rake thinking_sphinx:start</code></pre>
<p>After this you'll be able to search on your object. So using script/console</p>
<pre><code>@searched_pages = Page.search("query")</code></pre>
<p>will return what you're looking for!</p>
<h3>Setting up Dreamhost</h3>
<p>First things first you need to install Sphinx in your local area, as posted <a href="http://hughevans.net/2009/03/10/thinking-sphinx-dreamhost">by Hugh Evans</a>:</p>
<pre><code>cd ~/
mkdir -p local
wget http://sphinxsearch.com/downloads/sphinx-0.9.8.1.tar.gz
tar -xzf sphinx-0.9.8.1.tar.gz
cd sphinx-0.9.8.1/
./configure --prefix=$HOME/local/ --exec-prefix=$HOME/local/
make
make install</code></pre>
<p>then set up the PATHs</p>
<pre><code>echo "export PATH="$PATH:~/local/bin"" >> ~/.bash_profile
source ~/.bash_profile</code></pre>
<p>You can choose to set up a CRON task at this point too, but I'm not going into that.</p>
<p>Also at this point in the there's talk of using Sphinx being anti TOS in DH's eyes... but we'll see does the process get killed or not!</p>
<h3>Configuring Sphinx for DH</h3>
<p>Create a file called sphinx.yml in the RAILS_ROOT/config/ folder.</p>
<p>Because Dreamhost uses an externally referenced MySQL server instead of localhost you need to set up the sql_* parameters:</p>
<pre><code>
  sql_host: "mysql.YOURDOMAIN"
  sql_port: 3306
  sql_user: "USER"
  sql_password: "PASSWORD"
  sql_database: "DATABASE"
</code></pre>
<p>And because you installed Sphinx in your local area:</p>
<pre><code>
  bin_path: '/home/YOURUSERNAME/local/bin'
</code></pre>
<p>Finally, after setting whatever memory/fine tuning settings you wish/require set up the locations for the Sphinx files:</p>
<pre><code>
  config_file: "/home/YOURUSERNAME/DOMAIN.co.uk/shared/production.sphinx.conf"
  searchd_log_file: "/home/YOURUSERNAME/DOMAIN.co.uk/shared/log/searchd.log"
  query_log_file: "/home/YOURUSERNAME/DOMAIN.co.uk/shared/log/searchd.query.log"
  pid_file: "/home/YOURUSERNAME/DOMAIN.co.uk/shared/log/searchd.production.pid"
  searchd_file_path: "/home/YOURUSERNAME/DOMAIN.co.uk/shared/db/sphinx"
</code></pre>
<p>That should be you ready to start deploying.</p>
<h3>Deploying</h3>
<p>Using Git + Capistrano for deployment (and Passenger for the http server) my deploy.rb's namespace area looks like this:</p>
<pre><code>
namespace :deploy do
  task :restart do
    after_symlink
    restart_sphinx
    run "touch #{deploy_to}/current/tmp/restart.txt"
  end

  task :start do
    # nothing  (this avoids the 'spin' script issue)
  end

  desc "Re-establish symlinks"
  task :after_symlink do
    run <<-CMD
      rm -fr #{release_path}/db/sphinx &#038;&#038;
      ln -nfs #{shared_path}/db/sphinx #{release_path}/db/sphinx
    CMD
  end

  desc "Stop the sphinx server"
  task :stop_sphinx , :roles => :app do
    run "cd #{current_path} &#038;&#038; rake thinking_sphinx:stop RAILS_ENV=production"
  end

  desc "Start the sphinx server"
  task :start_sphinx, :roles => :app do
    run "cd #{current_path} &#038;&#038; rake thinking_sphinx:configure RAILS_ENV=production &#038;&#038; rake thinking_sphinx:index RAILS_ENV=production &#038;&#038; rake thinking_sphinx:start RAILS_ENV=production"
  end

  desc "Restart the sphinx server"
  task :restart_sphinx, :roles => :app do
    stop_sphinx
    start_sphinx
  end

end
</code></pre>
<p>There's probably a neater way to do this, but basically this makes sure Sphinx's indexes and conf files live in the shared deployment folder.</p>
<p>I recommend you try all this in a staging area first, obviously... and you can use Dreamhost's control panel to set up a staging subdomain with a new database in whatever fashion you prefer.</p>
<p>Any problems with this script flag them up, please! This is as much for my future reference as you googlies out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidlowry.co.uk/279/simple-thinking-sphinx-on-dreamhost/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Uninitialized Constant Error (Mental Note)</title>
		<link>http://www.davidlowry.co.uk/114/ruby-uninitialized-constant-error-mental-note/</link>
		<comments>http://www.davidlowry.co.uk/114/ruby-uninitialized-constant-error-mental-note/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 15:37:28 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[learning]]></category>
		<category><![CDATA[meaningful labor]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[script generate]]></category>
		<category><![CDATA[uninitialized constants]]></category>

		<guid isPermaLink="false">http://davidlowry.co.uk/?p=114</guid>
		<description><![CDATA[The reason I always get initialized constant errors occurring in Ruby is accidentally naming models the plural form of the database table name and not letting Rails automatically create the pluralisations. script/generate scaffold contact_skill contact_id:integer skill_id:integer description:string not script/generate scaffold contact_skills ... (This reason is one I constantly forget, and Google never helps me. Now...<a href="http://www.davidlowry.co.uk/114/ruby-uninitialized-constant-error-mental-note/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p>The reason I always get initialized constant errors occurring in Ruby is accidentally naming models the plural form of the database table name and not letting Rails automatically create the pluralisations.</p>
<p><code>script/generate scaffold contact_skill contact_id:integer skill_id:integer description:string </code><br />
not<br />
<code>script/generate scaffold contact_skills ... </code></p>
<p>(This reason is one I constantly forget, and Google never helps me. Now I will not forget)</p>
<p>&#8212;&#8211;</p>
<p>edit: I&#8217;ve had a LOAD of google hits from this search query in the last few days, if this helps you <em>or doesn&#8217;t</em> please leave a comment to make this article more complete.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidlowry.co.uk/114/ruby-uninitialized-constant-error-mental-note/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Sony Ericsson Phone Mac Crash Bug</title>
		<link>http://www.davidlowry.co.uk/105/sony-ericsson-phone-mac-crash-bug/</link>
		<comments>http://www.davidlowry.co.uk/105/sony-ericsson-phone-mac-crash-bug/#comments</comments>
		<pubDate>Thu, 31 Jan 2008 15:46:48 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[apple]]></category>
		<category><![CDATA[brands]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[reciprocal affection]]></category>

		<guid isPermaLink="false">http://davidlowry.co.uk/sony-ericsson-phone-mac-crash-bug/</guid>
		<description><![CDATA[Courtesy of this fella http://www-users.kawo2.rwth-aachen.de/~razzfazz/k750igrabber/ and thanks to this fella (via google search) http://www.mattheweaves.co.uk/2005/10/25/sony-ericsson-k750i-quickshare-mobile-phone-mac-osx-104-tiger-kernal-panic-crash-fix/ it appears there is indeed a way to stop my k800i crashing whenever I unplug it&#8217;s usb lead from my macbook pro. This little driver package disables the unused ports in the usb interface (with the sony ericsson devices). Next time...<a href="http://www.davidlowry.co.uk/105/sony-ericsson-phone-mac-crash-bug/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Courtesy of this fella http://www-users.kawo2.rwth-aachen.de/~razzfazz/k750igrabber/ and thanks to this fella (via google search)</p>
<p>http://www.mattheweaves.co.uk/2005/10/25/sony-ericsson-k750i-quickshare-mobile-phone-mac-osx-104-tiger-kernal-panic-crash-fix/</p>
<p>it appears there is indeed a way to stop my k800i crashing whenever I unplug it&#8217;s usb lead from my macbook pro. This little driver package disables the unused ports in the usb interface (with the sony ericsson devices). Next time I reboot I&#8217;ll confirm if it works or not, but this is good &#8211; hopefully I can recharge my phone and not risk a black curtain of doom each time!</p>
<p>&#8211;update&#8211;</p>
<p>So it appears it isn&#8217;t working (at all) but I&#8217;ll look into it again soon. Hope it might work for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidlowry.co.uk/105/sony-ericsson-phone-mac-crash-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting around QOL (aka JavaScript execution in Safari)</title>
		<link>http://www.davidlowry.co.uk/89/getting-around-qol/</link>
		<comments>http://www.davidlowry.co.uk/89/getting-around-qol/#comments</comments>
		<pubDate>Wed, 10 Oct 2007 09:57:46 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[accessibility]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[xhtml]]></category>
		<category><![CDATA[QOL]]></category>
		<category><![CDATA[queens university belfast]]></category>

		<guid isPermaLink="false">http://davidlowry.co.uk/getting-around-qol/</guid>
		<description><![CDATA[QOL is Queen&#8217;s University Belfast&#8217;s student intranet system which allows centralised delivery of lecture content and course material to enrolled students. Unsurprisingly it doesn&#8217;t work properly in Safari as it relies heavily on (old school) JavaScript to build page content (not just modify its appearance in a delightfully DOM manner) node_1 = node.addItem(new TreeNode('node_1','doc.gif','doc.gif','',false,true)); element...<a href="http://www.davidlowry.co.uk/89/getting-around-qol/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.qol.qub.ac.uk/">QOL</a> is Queen&#8217;s University Belfast&#8217;s student intranet system which allows centralised delivery of lecture content and course material to enrolled students. Unsurprisingly it doesn&#8217;t work properly in Safari as it relies heavily on (old school) JavaScript to build page content (not just modify its appearance in a delightfully DOM manner)</p>
<pre>node_1 = node.addItem(new TreeNode('node_1','doc.gif','doc.gif','',false,true));
element = node_1.addElement(new elemHyperLink('Student Handbook-2007-08.doc','JavaScript:downloadResource(289314);','','','Student Handbook'));
element = node_1.addElement(new elemText('(92.50 KB)',''));
element = node_1.addElement(new elemImageLink('Download Times', '../images/clock.gif','',0,'top','JavaScript:showDownloadTimes(94720,289314)'));</pre>
<p>Yuck. From the image below you can see the Lectures folder has been clicked to fold it out, but due to the JavaScript issue it does not.<img src="/share/qol_javascript.png" alt="Queen's Online has some accessibility issues with javascript" height="269" width="340" />You can see there is an element: JavaScript:downloadResource(289314); in the code which is the type of link behind any given resource. I&#8217;ve never noticed this before, but if you extract that link from the page source (Cmd+Alt+U in Safari, Cmd+U in Firefox) and enter it into the Page Address bar, click enter, it will execute the JavaScript right there and download the file to your default location.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidlowry.co.uk/89/getting-around-qol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

