Martinicity: Tag rails http://martinicity.net/articles_controller.rb/tag?tag=rails en-us 40 Mike Blake The Philanthropic Programmers <p>Something fantastic is happening in the <a href="http://www.rubyonrails.org/">Ruby and Rails</a> World.</p> <p>![](/images/ruby_shades.jpg)</p> <ol> <li>Do unto others</li> </ol> <p>Open source developers know from experience that what goes around comes around. Contributions to projects like Rails invariably come back to benefit everyone, including the contributor. The community is now thinking on a larger scale. Frameworks come and go, but <a href="http://pragdave.pragprog.com/pragdave/2007/05/rails_is_love.html">love</a> is timeless .</p> <ol> <li>The Summer of Love <p>is focusing his power on <a href="http://hacketyhack.net/">Hackety Hack</a> an amazing application that teaches children programming. And it&#8217;s likely more zeitgeist than coincidence that a professor from <span class="caps">MIT</span>, where the generous open source license originated, has started the noble project called <a href="http://laptop.org">One Laptop per Child</a> . Developers everywhere like <a href="http://slash7.com/articles/2006/4/17/teaching-rails-for-charity-in-d-c">Amy Hoy</a> are giving back in order to share the passion.</p></li> </ol> <p>![](/images/rails_logo_heart.png)</p> <p>Chad Fowler has challenged the entire community to harness our newfound passion to do <a href="http://www.chadfowler.com/2007/5/19/changing-the-world">great things</a>. And the community has only just begun to respond. During RailsConf over <a href="http://www.clarkware.com/cgi/blosxom/2007/05/22#RailsConf07">$33,000</a> was raised for <a href="http://pragmaticstudio.com/donate/">charity</a>.</p> <h3><a href="http://www.lse.co.uk/ShowbizNews.asp?Code=WV31893G&#38;headline=paris_hilton_shortlisted_for_nun_role">Witness the Transformation</a></h3> <p>Software evolves rapidly. Humanity seems to evolve at a slower pace, but if you watch carefully, with ruby colored glasses , you can see it happening.</p> Tue, 22 May 2007 21:16:00 +0000 urn:uuid:5cbbdb9a-35d6-4c26-aed0-fdf039d89942 Mike Blake http://martinicity.net/articles/2007/05/22/the-philanthropic-programmers Create Pray ruby rails love Hibernate 3.x Migration Guide <strong>Update 4/30</strong> <blockquote> <em>I finally did find the real <a href="http://www.hibernate.org/250.html">Hibernate 3 Migration Guide</a>. My fun with Hibernate is over though, I&#8217;ve found an easier persistence solution with the clients homegrown method. I believe every single java project I&#8217;ve worked on in the past 10 years has used a different and unique Persistence mechanism. My favorite was <a href="http://www.eclipsecon.org/2007/index.php?page=presenters/#Francois_Granade">Francois&#8217;</a> PersistentEntity at <a href="http://finance.groups.yahoo.com/group/viagone/">ViaFone</a>.</em> </blockquote> <hr /> I&#8217;ve been strugling for the last day trying to get a Java application working, mainly, trying to get Hibernate to see a DataSource. Now I&#8217;m at a point where I get this error message: <blockquote> <p>[junit] (cfg.Configuration 1312) configuring from resource: /hibernate.cfg.xml</p> </blockquote> <blockquote> <p>[junit] (cfg.Configuration 1289) Configuration resource: /hibernate.cfg.xml</p> </blockquote> <blockquote> <p>[junit] (util.DTDEntityResolver 30 ) Don&#8217;t use old DTDs, read the <b>Hibernate 3.x Migration Guide!</b></p> </blockquote> <p>A quick search does not turn up a Hibernate Migration Guide, so I decided to create this one:</p> <h3>The Hibernate 3.x Migration Guide</h3> <p><b>1. Get <a href="http://www.ruby-lang.org/en/">Ruby</a></b></p> <p><b>2. Install <a href="http://www.rubyonrails.org/down">Rails</a></b></p> <p><b>3. Rebuild your application.</b></p> <p><b>4. Have fun again!</b></p> Thu, 26 Apr 2007 11:30:00 +0000 urn:uuid:4211efe2-2393-422e-af13-478696806a85 Mike Blake http://martinicity.net/articles/2007/04/26/hibernate-3-x-migration-guide Create ruby rails hibernate migration http://martinicity.net/articles/trackback/889 Testing a Non-Rails Application Using Rails <p>Rails developers working on enterprise software projects are suprised to discover the lack of automated tests in many mature web applications. As frustrating as this can be, A lack of automated tests is also a tremendous opportunity to </p> <pre><code>1. <b>Learn your non-rails applications underlying database structure.</b> 2. <b>Demonstrate to a devlopment team the power of The Rails Framework.</b> 2. <b>Encourage automated testing.</b></code></pre> <p>Rails may be the quickest path to automate some basic test of your non-rails applications data model. These steps will get you all set up to write your automated tests in Ruby.</p> <p>1. <a href="#connect">Connect to Your Enterprise Database</a> 2. <a href="#conventions">Set DB Conventions</a> 3. <a href="#safety">Safety Net</a> 4. <a href="#schema">Duplicate the Schema</a> 4. <a href="#extract">Extract Development Data</a></p> <h4 id='connect'>I. Connect to Your Enterprise Database from Rails.</h4> <p> <p>Download and install any os driver, ruby gems, and rails adapters needed to connect to your database:</p> <blockquote> <p><a href="http://wiki.rubyonrails.com/rails/pages/MySQL">MySQL</a> <br><a href="http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0606dumbill/?ca=dgr-lnxw01DB24RubyonRails"><span class="caps">DB2</span></a> <br><a href="http://www.oracle.com/technology/pub/articles/saternos-ror-faq.html">Oracle</a> <br><a href="http://wiki.rubyonrails.org/rails/pages/HowToSetupSybaseAdapterOnRails">Sybase</a> <br><a href="http://wiki.rubyonrails.org/rails/pages/HowtoConnectToMicrosoftSQLServer">SQLServer</a> <br><a href="http://wiki.rubyonrails.org/rails/pages/PostgreSQL">PostgreSQL</a> <br><a href="http://wiki.rubyonrails.org/rails/pages/Firebird">Firebird</a></p> </blockquote> <h4 id='conventions'>II. Set Your Existing Database Conventions </h4> <p> <p>, Recipie #16 can walk you through this very quickly.</p> Identify any conventions used by your legacy database. Application wide conventions can be set in the <code>config/environment.rb</code> file. They are specified by calling the appropriate class methods available on <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html">ActiveRecord::Base</a>. The methods you need to call depend on how your database is configured: <pre> ActiveRecord::Base.table_name_prefix 'myapp_' ActiveRecord::Base.table_name_suffix = '_def' ActiveRecord::Base.sequence_name = 'dev_company' ActiveRecord::Base.pluralize_table_names = false ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore # or :table_name </pre> <h4 id='safety'><span class="caps">III</span>. Safety Net</h4> <p> <p>Load the <a href="http://agilewebdevelopment.com/plugins/safety_net">SafetyNet Plugin</a> into your rails app to prevent from destroying your development database.</p> <h4 id='schema'>IV. Duplicate Your Database Schema</h4> <p> <p>OK , with your dev and test development databases set correctly, let&#8217;s try duplicting the schema.</p> <code>rake db:test:clone</code> If this works right off the bat, you&#8217;re one of the lucky ones. Skip to <a href="#extract">Extracting Development Data</a> <p> <p>If you recieved errors from the clone command, do steps A through C .</p> <p>A. You can now correct any errors you may have had by manually modifying schema.rb . See <a href="/articles/2007/03/29/rails-on-oracle">Oracle Errors</a> for some problems I had with the Oracle Database. Since schema.rb is generated, it&#8217;s a good idea to rename it when you modify it manually.</p> B. The clone command above may have started loading data; you need may to purge the test db. <code> rake db:test:purge </code> <p>C. Tell rake to load your new schema file by passing the new name, relative to <span class="caps">RAILS</span>_ROOT in the <span class="caps">SCHEMA</span> environment variable:</p> <code> rake db:schema:load RAILS_ENV=test SCHEMA=db/oracle\_schema.rb </code> <h4 id='extract'>V. Extracting Development Data</h4> <p> <p>Copy the code from Rails Recipe #42: <a href="http://media.pragprog.com/titles/fr_rr/code/CreateFixturesFromLiveData/lib/tasks/extract_fixtures.rake">extract_fixtures.rake</a> to your <span class="caps">RAILS</span>\_ROOT/lib/tasks directory.</p> <blockquote> <p><b><span class="caps">TIP</span>: If your develoment database has lots of data, Modify the <span class="caps">SQL</span> Select in this code to limit the amount of data you copy using the <span class="caps">SQL</span> limit statement:</b></p> </blockquote> <blockquote> <code>sql = "SELECT * FROM %s limit = 100"</code> </blockquote> <blockquote> <p>Or for a proprietary database, the equivalent command:</p> </blockquote> <blockquote> <code>sql = "SELECT * FROM %s WHERE ROWNUM&lt;=100"</code> </blockquote> Then run <code> rake extract_fixtures </code> <p> <ol> <li>You&#8217;ve made your testbed so you can lie in it.</li> </ol> <p>You now have and exact copy of your development database, and a collection of sample data in Yaml format, and are ready to begin writing some Unit Tests. Stay tuned for some examples. Rail on! <span> <script type="text/javascript"> digg_url = &#8216;http://martinicity.net/articles/2007/03/31/testing-a-non-rails-application-using-rails&#8217;; digg_title = &#8216;Testing a Non-Rails Application Using Rails&#8217;;</script> <script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script> </span></p> Sat, 31 Mar 2007 13:11:00 +0000 urn:uuid:ae1917e2-4a47-4fa2-be71-d57109445b46 Mike Blake http://martinicity.net/articles/2007/03/31/testing-a-non-rails-application-using-rails Create rails ruby unit tests database yml rake Rails on Oracle <p>When rails on jruby and connecting to Oracle via <span class="caps">JDBC</span> ,the following error eventually appears:</p> Update: <code> java.sql.SQLException: Io exception: Broken pipe </code> <p><b>Fix:</b></p> You need a dedicated connection from Oracle. Change the url: line in database.yml from <code> url: jdbc:oracle:thin:@localhost:1521:XE to url: jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (ADDRESS =(PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))) (CONNECT_DATA =(SERVER = DEDICATED) (SID = XE))) </code> <ol> <li>Workarounds</li> </ol> <p>Duplicating an existing Oracle Database using the build in task <code>rake db:test:clone</code> presented a few problems. Here&#8217;s what I did to work around them.</p> <ol> <li><span class="caps">ORA</span>-01727: numeric precision specifier is out of range</li> </ol> <code> OCIError: ORA-01727: numeric precision specifier is out of range (1 to 38): CREATE TABLE employee (id NUMBER(38) NOT NULL PRIMARY KEY, created_date DATE DEFAULT NULL, start_date DATE DEFAULT NULL, job_id DECIMAL DEFAULT NULL, total_hours NUMBER(126) DEFAULT NULL) </code> <p><b>Fix:</b></p> <p>For some reason whne rails dumps the schema, it reports Oracle type <span class="caps">FLOAT</span> as <acronym title="126">NUMBER</acronym>, so you just need to changed that back to <span class="caps">FLOAT</span> if you want to import that schema.</p> ###OCIError: <span class="caps">ORA</span>-00972 identifier is too long: <code> OCIError: ORA-00972: identifier is too long: CREATE SEQUENCE gametime\_responsibility\_ref\_seq START WITH 10000 </code> <p><b>Fix:</b></p> <p>Rails tries to create sequences in Oracle to handle <span class="caps">AUTOINCREMENT</span> id fields. It uses <span class="caps">TABLE</span>_NAME = &#8216;_SEQ&#8217; for the sequence name. If a sqequence name is too long then you have to shorten it in schema.rb .</p> <h3>OCIError: <span class="caps">ORA</span>-00907: missing right parenthesis</h3> <code> OCIError: ORA-00907: missing right parenthesis: CREATE TABLE board (id NUMBER(38) NOT NULL PRIMARY KEY, name VARCHAR2(150) NOT NULL, name VARCHAR2(150) NOT NULL, parent VARCHAR2(150) DEFAULT NULL, log_level DECIMAL DEFAULT NULL, modified_date DATE(6) DEFAULT NULL, token DECIMAL DEFAULT NULL) </code> <p><b>Fix:</b></p> <p>For some reason Rails assigns Date fields a size in schema.rb . You&#8217;ll need to change all occurances of <acronym title="6">DATE</acronym> to <span class="caps">DATE</span> .</p> Thu, 29 Mar 2007 21:15:00 +0000 urn:uuid:1fd946dc-17d2-40fd-abf8-2b4dd3474377 Mike Blake http://martinicity.net/articles/2007/03/29/rails-on-oracle Create rails oracle schema rb Undefined method assert_select <p>The <a href="http://blog.labnotes.org/2006/09/04/assert_select-cheat-sheet/">assert_select</a> method for testing views was added in <a href="http://weblog.rubyonrails.org/2006/11/26/1-2-new-in-actionpack">Rails 1.2</a>, so the first thing to check if you&#8217;re getting the error below is that you&#8217;ve upgraded to 1.2</p> <pre> &gt; 2) Error: &gt;test_expire_old_ads(AdExpirationTest): &gt;NoMethodError: undefined method `assert_select' for #&lt;ActionController::Integration::Session:0x378ae98&gt; &gt; c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/test_process.rb:432:in `method_missing' </pre> <p>Now I upgraded to 1.2 and still kept getting the above error. The problem was that my application was set to use Rails 1.1.6 in the config/environment.rb file:</p> <div class="typocode"><pre><code class="typocode_ruby "><span class="comment"># Specifies gem version of Rails to use when vendor/rails is not present</span> <span class="constant">RAILS_GEM_VERSION</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">1.1.6</span><span class="punct">'</span></code></pre></div> <p>So the solution is to comment out that line, or alternatively ,set it to the latest version. Also make sure you&#8217;re not frozen to a specific Rails version in the vendor directory.</p> Mon, 19 Feb 2007 18:23:00 +0000 urn:uuid:6ddf5409-4a21-4502-a1d6-b9dc8733aedc Mike Blake http://martinicity.net/articles/2007/02/19/undefined-method-assert_select Create rails testing assert_select NoMethodError The Unselfish Act of Writing Tests <p> ####ORUG At this months <a href="http://www.orug.org">Orlando Ruby User&#8217;s Group</a> meeting, I did my <a href="http://www.apptrain.net/testing/img0.html">Testing on Rails</a> presentation. In it I touched on just some of the benefits of having an automated suite of Unit, Functional, and Integration tests. <p> <p>!<a href="http://www.apptrain.net/testing/img4.jpg">Benefits of Testing</a></p> <p> Later I thought of another major benefit. Tests help other people on the project, in addition to the author of the test. <ol> <li>Good Habits are Contagious</li> </ol> <p>Recently I had the opportunity to work on a project with <a href="http://blog.harrisreynolds.net/">Harris Reynolds</a>. Harris did something very important that sadly, you don&#8217;t see on every Rails Project. He wrote a unit test. Lot&#8217;s of us wrote tests , but this particular test stood out for two reasons. The first was when it was written.</p> <ol> <li>Writing Tests First</li> </ol> <p>Harris had found a bug in a model object that I had created. To notify me, he checked in an assertion that failed, then emailed me. This saved me time searching thought and remembering code I had written. I simply typed <code>rake test:recent</code> which runs all recently changed tests in a Rails application . The error took me right to the problem, and I immediately fixed the bug.</p> <ol> <li>Testing a Deployment</li> </ol> <p>The second reason I remember this test happened at least a month later. The application was being deployed to a new machine, and the test failed. The particular model object being tested pointed to a different database that the rest of the application, and it turned out that that database hadn&#8217;t been installed correctly on the new machine. Harris&#8217;s test told the problem straight away, and saved time once again.</p> <p><b>Unit Testing: the gift that keeps on giving.</b></p> <p>Thanks Harris!</p> Wed, 14 Feb 2007 21:07:00 +0000 urn:uuid:d3a23de4-6d31-4890-aaf1-7e6e04e61f32 Mike Blake http://martinicity.net/articles/2007/02/14/the-unselfish-act-of-writing-tests Create rails ruby unit tests ORUG Rails SafetyNet <h2><b>Curent Mood:</b> Bored with Deleting the Production Database</h2> <p> <p>I thought I was the only clown working on Rails projects and ignoring the <a href="http://dev.rubyonrails.org/svn/rails/applications/plugins/config/database.example.yml">warning in config/database.yml</a> and using rake to wipe out perfectly good databases. But I&#8217;ve seen it happen to others now, and enough&#8217;s enough. So I created the <h3>SafetyNet Plugin</h3>.</p> <p>You can install Safety Net in your app, by running</p> <code> ruby script/plugin install svn://rubyforge.org/var/svn/apptrain/trunk/vendor/plugins/safety_net </code> <p>That&#8217;s it. Now, If the <b>test</b> database points to the same database as <b>development</b> or <b>production</b> running rake will display the following message:</p> <p>!<a href="/images/safety_net.png">safety net</a></p> <pre> rake aborted! The name of your test database matches production or development. </pre> <h3>How it Works</h3> <p> <p>Purging the poor innocent database is prevented in two ways.</p> <p>1. By adding a prerequisite check to the rake script that normaly performs this task. 2. By modifying the fixtures method on ActiveRecord to avoid the same fate when running individual tests with the ruby command.</p> Wed, 07 Feb 2007 21:14:00 +0000 urn:uuid:7d6f994d-0845-41c7-a8ea-09a9157c94db Mike Blake http://martinicity.net/articles/2007/02/07/safety-net Create safetynet safetynet apptrain rails plugin rake rails test database Keeping your Rails Codebase Clean with SVN Externals <p>As you develop a nice well organized Rails application, you&#8217;re sure to utilize some of the great open source code already written by Rails developers.</p> <p>A common practice is to install a plugin or some other code into your application, then add that code to subversion. Some problems with this are that a checkout of your app now takes longer, and if the plugin improves, you are using old code in your application. A neater solution is to access third party code using <i><span class="caps">SVN</span> Externals.</i></p> <p>Using <a href="http://tortoisesvn.tigris.org">Tortoise <span class="caps">SVN</span></a>, right click inside the vendor directory of your app, then choose &#8216;properties&#8217; from the menu.</p> <p>!<a href="/images/svn_properties.gif">svn properties</a></p> <p>Click the &#8216;Subversion&#8217; Tab with the little turtle. Then choose <b>svn:externals</b> from the dropdown in the middle of the screen.</p> <p>!<a href="/images/externals.gif">svn:externals</a></p> Now in the text area, enter the plugin you want to include. <code>acts_as_authenticated&amp;nbsp;http://svn.techno-weenie.net/projects/plugins/acts\_as\_authenticated </code> <p>The format here is important, the first word the entry, before the first space will be the <b>directory name</b> where the code will live. After the first space is the <b>url for the repository</b>. Click &#8216;OK to save the external. Then go up one directory and do a checkout of the &#8216;vendor&#8217; directory.</p> <p>Now:</p> <p>1. If the plugin code changes, you&#8217;ll receive the new code whenever you do an Update. 1. Additional coders who check out the repository have the option to &#8216;Omit externals&#8217; if they don&#8217;t need the portion of the code you&#8217;ve included. 1. There&#8217;s a clear distinction in your repository between the code you&#8217;re maintaining, and third party code.</p> Wed, 31 Jan 2007 16:25:00 +0000 urn:uuid:dee6f8d3-128c-49df-abbe-ffc8ff243570 Mike Blake http://martinicity.net/articles/2007/01/31/keeping-your-rails-codebase-clean-with-svn-externals Create rails svn externals tortoise subversion New Rails Site: FanNation <p>The folks at <a href="http://www.fannation.com">FanNation</a> have done something that no one else in the sports world has done. <span style="float:right;"> <map id ="favorite_team_map" name="favorite_team_map"><area shape ="rect" coords ="0,0,160,80" href="http://www.fannation.com" target ="_blank" alt="FanNation: The Republic of Sport" title="FanNation: The Republic of Sport" /><area shape ="rect" coords ="10,82,150,102" href ="http://www.fannation.com/users/view_user_profile/530" target ="_blank" alt="mblake" title="mblake" /><area shape ="rect" coords ="10,102,150,260" href ="http://www.fannation.com/player_trackers/team_overview/530" target ="_blank" alt="mblake's Favorite Teams" title="mblake's Favorite Teams" /><area shape ="rect" coords ="0,265,160,300" href="http://www.fannation.com/remote/favorite_teams" target ="_blank" alt="Get your own Favorite Teams Widget!" title="Get your own Favorite Teams Widget!" /></map><image src="http://www.fannation.com/remote/favorite_teams_image/530" alt="mblake's Favorites Teams" usemap="#favorite_team_map" border="0" /> </span> They&#8217;ve created a <a href="http://www.fannation.com">comprehensive site</a> for sports fans to gather and share information about their favorite teams and players (fantasy too) , and it&#8217;s all free.</p> <p>I spent some time with the <a href="http://www.fannation.com/fannation/about">developers at FanNation</a> working on the map on the home page. They are a sharp bunch, and I&#8217;m constantly amazed at the features they are adding each time I visit. Fannation is now one of the biggest websites around developed on <a href="http://www.rubyonrails.org/">Ruby on Rails</a>. Tag clouds, in place edits, Dynamic news updates using <span class="caps">AJAX</span> calls, personalized blogging. This site is a tremendous example of practical usage of some of the latest technologies. And it&#8217;s loads of fun.</p> <p>There&#8217;s tons of other functionality I haven&#8217;t explored yet, but definitely keep an eye on FanNation!</p> Thu, 21 Sep 2006 17:25:00 +0000 urn:uuid:a9b2f624-31cf-4d7a-b807-636c42cdc9c0 Mike Blake http://martinicity.net/articles/2006/09/21/new-rails-site-fannation Create fannation ruby rails Attend a Hackfest! <p>If you&#8217;re a programmer like me you&#8217;re a little intimidated by the <a href="http://www.flickr.com/photos/45241414@N00/sets/72157594276834801/">Hackfest</a>. That is , you&#8217;ve had several programming gigs over the years, attended a few more day <a href="http://www.pluckthepetal.com/blackdaisies/2004/02/long_meetings/">long meetings</a> than you&#8217;d have liked, and fear that you&#8217;ll be embarrassed by your perceived lack of skill compared to the young <a href="http://www.rubyonrails.org/core">whiz kids</a> with a burning passion for creating software.</p> <p>We&#8217;ll let me assure you, the spirit of the hackfest is one of <a href="http://redhanded.hobix.com/cult/everyoneIsHereInTheFuture2.html">learning and cooperation</a> and not at all that of oneupmanship or competition. At least not at the <a href="http://www.orug.org"><span class="caps">ORUG</span></a> .</p> <p>I had a blast this past weekend and learned a lot from my piers, and left with the feeling that I had helped out as well.</p> <p>So my advice, fear not and attend a Hackfest!</p> Tue, 12 Sep 2006 14:30:00 +0000 urn:uuid:cc7c8d73-4271-44eb-860e-545367b0a9d6 Mike Blake http://martinicity.net/articles/2006/09/12/attend-a-hackfest Create ruby rails hackfest ORUG