<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>The Bacon Bear Blog: Category rails</title>
    <link>http://www.baconbear.com/articles/category/rails</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>So far so good!</title>
      <description>&lt;p&gt;Couple days into playing with RubyCocoa and its so far so good!  The toughest part of learning any new framework is learning the framework&amp;#8217;s terminology and Cocoa&amp;#8217;s no exception.  Nibs, outlets, selectors, bindings&amp;#8230;  All foreign concepts if you&amp;#8217;ve never worked in Cocoa before.  Luckily the online tutorials are fairly good and Apple&amp;#8217;s documentation is excellent.&lt;/p&gt;

&lt;p&gt;The only problem i&amp;#8217;ve run into so far turned out to be my own damned fault.  I&amp;#8217;m doing a lot of the RESTful heavy lifting using &lt;a href="http://svn.rubyonrails.org/rails/trunk/activeresource/"&gt;ActiveResource&lt;/a&gt; and it really doesn&amp;#8217;t like receiving NSStrings as parameters.  So much so that it crashes the App.  No good.  No good at all.  &lt;/p&gt;

&lt;p&gt;Remember kids, NSString != ruby string.  to_s is your friend.&lt;/p&gt;</description>
      <pubDate>Wed, 19 Sep 2007 19:03:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:dcc0f1f4-e874-4e05-9c1e-cedb770dbefc</guid>
      <author>Jeff</author>
      <link>http://www.baconbear.com/articles/2007/09/19/so-far-so-good</link>
      <category>rails</category>
    </item>
    <item>
      <title>ActsAsLocateable released</title>
      <description>&lt;p&gt;Introducing my 2nd adventure into Rails plugins: ActsAsLocateable!&lt;/p&gt;

&lt;p&gt;ActsAsLocateable is an easy way to give any model the ability to be found by location.  For instance, say you have a model Store which is associated with a zip code indicating where that Store is located.  To find all the stores within a specific radius of another ZipCode, you can do a&lt;/p&gt;

&lt;pre&gt;
Store.find_within_radius(10, 12345)     # Find all stores within 10 miles of zip code 12345
&lt;/pre&gt;

&lt;p&gt;Install ActsAsLocateable in the normal way&lt;/p&gt;

&lt;pre&gt;
script/plugin install http://svn.baconbear.com/rails_plugins/acts_as_locateable/trunk/
&lt;/pre&gt;

&lt;p&gt;Be sure to read the associated README file after the install for details on installation, setup, and usage.&lt;/p&gt;

&lt;p&gt;ActsAsLocateable is based heavily on the &lt;a href="http://zipcodesearch.rubyforge.org/"&gt;ZipCodeSearch plugin&lt;/a&gt; by Doug Fales and utilizes much of the same code and data.  Special thanks to Doug for making his code freely available!&lt;/p&gt;</description>
      <pubDate>Fri, 29 Dec 2006 17:54:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:5f8ec1d7-fe33-45aa-bf8f-1b8a8658367e</guid>
      <author>Jeff</author>
      <link>http://www.baconbear.com/articles/2006/12/29/actsaslocateable-released</link>
      <category>rails</category>
      <category>ActsAsLocateable</category>
      <category>plugins</category>
    </item>
    <item>
      <title>Found Feature: Nested Scopes</title>
      <description>&lt;p&gt;While browsing &lt;a href="http://blog.teksol.info/"&gt;A Single Programmer&amp;#8217;s Blog&lt;/a&gt; I came across this &lt;a href="http://blog.teksol.info/articles/2006/04/14/using-nested-scopes-to-not-repeat-yourself"&gt;article&lt;/a&gt; regarding a little known Rails feature (at least to me) called &amp;#8220;Nested Scope&amp;#8221;&lt;/p&gt;

&lt;p&gt;Ultimately it allows you to turn code like this:&lt;/p&gt;

&lt;pre&gt;
def self.find_by_active_and_paying(options={})
  conditions = Array.new
  conditions &lt;&lt; 'active = 1'
  conditions &lt;&lt; 'fee &gt; 0'
  conditions &lt;&lt; options[:conditions]
  self.find(:all, :conditions =&gt; [conditions.join(' AND '), options[:values])
end
&lt;/pre&gt;

&lt;p&gt;Into this:&lt;/p&gt;

&lt;pre&gt;
def self.find_by_active_and_paying(options={})
  active_scope do
    paying_scope do
      self.find(:all, options)
    end
  end
end

protected
def self.active_scope 
  with_scope(:find =&gt; {:conditions =&gt; 'active = 1'}) do
    yield
  end
end

def self.paying_scope 
  with_scope(:find =&gt; {:conditions =&gt; 'fee &gt; 0'}) do
    yield
  end
end
&lt;/pre&gt;

&lt;p&gt;Notice how the build up of conditions is gone and instead replaced with *_scope calls.  This has great implications for keeping with DRY principles.&lt;/p&gt;

&lt;p&gt;For a fuller explanation check out these articles: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blog.teksol.info/articles/2006/04/14/using-nested-scopes-to-not-repeat-yourself"&gt;A Single Programmers Blog&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.codyfauser.com/articles/2006/02/01/using-with_scope-to-refactor-messy-finders"&gt;Codey Fauser&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000754"&gt;Rails Docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
      <pubDate>Mon, 25 Dec 2006 21:58:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:e1ba5e5a-857d-4612-9e82-b4efc8245edc</guid>
      <author>Jeff</author>
      <link>http://www.baconbear.com/articles/2006/12/25/found-feature-nested-scopes</link>
      <category>rails</category>
    </item>
    <item>
      <title>Setting up a fresh OSX rails install</title>
      <description>&lt;p&gt;While setting up a fresh rails install on a basically virgin system, I stumbled across this &lt;a href="http://blog.duncandavidson.com/2006/04/sandboxing_rail.html"&gt;web site&lt;/a&gt; which goes over exactly that.  So, instead of having to figure this all out for myself (again) I could simply follow the instructions of another.&lt;/p&gt;

&lt;p&gt;All was not pain free however (why is the process never pain free?).  When trying to start mysql via launchctl, mysqld would not start.  In fact it didn&amp;#8217;t seem to do anything!  I could see the mysql.server start script being called but then it would just sit there.&lt;/p&gt;

&lt;p&gt;Since that route failed me, I then tried the more direct route of starting via /opt/local/lib/mysql5/bin/mysqld.  This seemed to start the server but it would then immediately stop.  Checking the logs, I found this:&lt;/p&gt;

&lt;pre&gt;
061224 23:09:52  mysqld started
061224 23:09:52 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive
061224 23:09:52  InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
061224 23:09:52  InnoDB: Starting log scan based on checkpoint at
InnoDB: log sequence number 0 36808.
InnoDB: Doing recovery: scanned up to log sequence number 0 43655
061224 23:09:52  InnoDB: Starting an apply batch of log records to the database...
InnoDB: Progress in percents: 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
InnoDB: Apply batch completed
061224 23:09:52  InnoDB: Started; log sequence number 0 43655
061224 23:09:52 [ERROR] /opt/local/libexec/mysqld: Can't find file: './mysql/host.frm' (errno: 13)
061224 23:09:52 [ERROR] /opt/local/libexec/mysqld: Can't find file: './mysql/host.frm' (errno: 13)
061224 23:09:52 [ERROR] Fatal error: Can't open and lock privilege tables: Can't find file: './mysql/host.frm' (errno: 13)
061224 23:09:52  mysqld ended
&lt;/pre&gt;

&lt;p&gt;Looking up the mysql/host.frm complaint with google led me to this &lt;a href="http://www.linuxquestions.org/questions/showthread.php?t=262262"&gt;thread&lt;/a&gt; on linuxquestions.  Bullet point #5 of &amp;#8220;Butt-Ugly&amp;#8220;&amp;#8216;s response solved my problem.  Looks like perhaps darwinports or myself had created this database with the root user instead of the mysql user causing mysqld to have permissions problems when trying trying to read files it needed.&lt;/p&gt;

&lt;p&gt;Removing the files in that directory followed by a restart of the server fixed the problem and I was on my way to mysql bliss.&lt;/p&gt;</description>
      <pubDate>Sun, 24 Dec 2006 21:55:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:75b2efdb-62e3-45a8-bc0a-53e1d291d197</guid>
      <author>Jeff</author>
      <link>http://www.baconbear.com/articles/2006/12/24/setting-up-a-fresh-osx-rails-install</link>
      <category>rails</category>
    </item>
    <item>
      <title>Acts As Flaggable updated</title>
      <description>&lt;p&gt;Just checked in a small update to Acts As Flaggable that includes the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Incorporates suggestion from bitbutter to allow add_flag to take either a Flag object (as before) or a Hash.  If passed a Hash it creates a Flag object from the hash you passed in.&lt;/li&gt;
&lt;li&gt;Added user_has_flagged? instance method to tell you if a user has flagged an object at all or flagged it with a specific flag&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Grab it from &lt;a href="http://svn.baconbear.com/rails_plugins/acts_as_flaggable/trunk/"&gt;trunk&lt;/a&gt; in the normal way!&lt;/p&gt;</description>
      <pubDate>Wed, 06 Dec 2006 13:34:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:cc770546-c79b-4075-b04a-9a00410a84de</guid>
      <author>Jeff</author>
      <link>http://www.baconbear.com/articles/2006/12/06/acts-as-flaggable-updated</link>
      <category>rails</category>
      <category>plugins</category>
      <category>acts_as_flaggable</category>
      <trackback:ping>http://www.baconbear.com/articles/trackback/10</trackback:ping>
    </item>
    <item>
      <title>OSX, SQLite3, and Rails</title>
      <description>&lt;p&gt;While preparing my travel laptop for rails work, I decided to give SQLite a try.  I figured, &amp;#8220;no problem&amp;#8221;, rails was simple enough to setup for MySQL, certainly it would be just as easy for SQLite.  I was &lt;em&gt;almost&lt;/em&gt; right.&lt;/p&gt;

&lt;p&gt;After setting up the database.yml file to use sqlite3 instead of mysql, I started seeing this problem:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;SQLite3::SQLException: near &amp;#8220;ADD&amp;#8221;: syntax error: ALTER TABLE blah blah blah&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After some googling, I found this &lt;a href="http://mcornick.org/stories/72"&gt;post&lt;/a&gt; at another blog site.  Basically it suggests ensuring you have the latest sqlite3 installed and the latest sqlite3-ruby gem.  After some &lt;a href="http://darwinports.opendarwin.org/"&gt;port&lt;/a&gt; magic and a quick gem update, I was totally up to date.&lt;/p&gt;

&lt;p&gt;But I STILL had the same problem!  Where did I go wrong?  I reread &lt;a href="http://mcornick.org/stories/72"&gt;Mark Cornick&amp;#8217;s post&lt;/a&gt; and noticed this little note at the bottom&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;BTW, hopefully everyone knows this by now, but sqlite3-ruby won’t even link against your libsqlite3 unless SWIG is installed. Without SWIG, you get an incredibly strange sqlite3-ruby that stopped living and became a crazy mixed-up zombie&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;SWIG?  I had never heard of SWIG before.  Well, I found it &lt;a href="http://www.swig.org/"&gt;here&lt;/a&gt;.  SWIG basically generates code that connects various high level languages (such as Ruby) to C/C++ libraries.  I&amp;#8217;m assuming this is how ruby creates it&amp;#8217;s bindings to sqlite3.&lt;/p&gt;

&lt;p&gt;Once I had SWIG installed, I did a quick uninstall and reinstall of sqlite3-ruby and I was in business, migrations and all.&lt;/p&gt;

&lt;p&gt;For more info on installing SQLite for use with rails, check out &lt;a href="http://wiki.rubyonrails.com/rails/pages/HowtoUseSQLite"&gt;this how to&lt;/a&gt; wiki page off the main rails wiki.&lt;/p&gt;</description>
      <pubDate>Wed, 06 Dec 2006 00:50:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:d832b296-f7ca-42c6-8deb-6ca6aa7cbd7e</guid>
      <author>Bacon Bear</author>
      <link>http://www.baconbear.com/articles/2006/12/06/osx-sqlite3-and-rails</link>
      <category>rails</category>
      <category>sqlite</category>
      <trackback:ping>http://www.baconbear.com/articles/trackback/9</trackback:ping>
    </item>
    <item>
      <title>Acts As Flaggable released</title>
      <description>&lt;p&gt;Here&amp;#8217;s my first stab at a rails plugin, small as it may be.&lt;/p&gt;

&lt;p&gt;Acts As Flaggable is a model modifier which allows flags to be associated with records.  Authors of community driven sites may find this useful for quickly adding the ability to mark records as &amp;#8220;spam&amp;#8221; or &amp;#8220;inappropriate&amp;#8221;.  There&amp;#8217;s also facilities in place to automatically take a specific action automatically if a record has been flagged a certain way too many times (Think auto-removing posts that have been marked as &amp;#8220;spam&amp;#8221;).&lt;/p&gt;

&lt;p&gt;To install:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;script/plugin install http://svn.baconbear.com/rails_plugins/acts_as_flaggable/trunk
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To prepare your database, create a migration with the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def self.up
   create_table :flags, :force =&gt; true do |t|
     t.column :flag, :string, :default =&gt; ""
     t.column :comment, :string, :default =&gt; ""
     t.column :created_at, :datetime, :null =&gt; false
     t.column :flaggable_id, :integer, :default =&gt; 0, :null =&gt; false
     t.column :flaggable_type, :string, :limit =&gt; 15,
       :default =&gt; "", :null =&gt; false
     t.column :user_id, :integer, :default =&gt; 0, :null =&gt; false
   end

   add_index :flags, ["user_id"], :name =&gt; "fk_flags_user"
 end

 def self.down
   drop_table :flags
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To prepare your model:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Model &lt; ActiveRecord::Base
    acts_as_flaggable

        protected
        # Flagged method is called after every add_flag.  This callback method
        # is totally optional and does not have to be included in the model
        def flagged(flag, flag_count)
            # Add code here to take action when flag reaches a certain flag_count
        end
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To use:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;p = Post.find(someid)
p.add_flag Flag.new(:flag =&gt; :spam, :comment =&gt; 'some comment')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you notice I used add_flag instead of the typical &amp;lt;&amp;lt; operator allowed by rails.  Currently it&amp;#8217;s a requirement that you use add_flag if you need the flagged callback to be called.  I&amp;#8217;ll probably fix this in a future checkin.&lt;/p&gt;

&lt;p&gt;Avid plugin users will recognize a lot of the code and README from the &lt;a href="http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/"&gt;Acts As Commentable&lt;/a&gt; plugin.  Thanks to the clear and readable manner Acts As Commentable was written in, it was a fairly easy change to morph the commentable plugin into the flaggable plugin I have here.  So, special thanks to Juixe for releasing a well written plugin!&lt;/p&gt;</description>
      <pubDate>Mon, 04 Dec 2006 01:32:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:d58bcfdf-7e21-4239-99d9-91077f547d84</guid>
      <author>Jeff</author>
      <link>http://www.baconbear.com/articles/2006/12/04/acts-as-flaggable-released</link>
      <category>rails</category>
      <category>plugins</category>
      <category>acts_as_flaggable</category>
      <trackback:ping>http://www.baconbear.com/articles/trackback/6</trackback:ping>
    </item>
  </channel>
</rss>
