<?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>Kevin Bedell on Internet Tech &#187; ruby</title>
	<atom:link href="http://www.kbedell.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kbedell.com</link>
	<description>Discussions on Ruby on Rails, Agile Development and the Boston Tech Scene.</description>
	<lastBuildDate>Mon, 09 Jan 2012 15:40:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>&#8216;seed&#8217; data versus &#8216;testing&#8217; data (and custom rake tasks) for Ruby on Rails</title>
		<link>http://www.kbedell.com/2011/03/15/seed-data-versus-testing-data-and-custom-rake-tasks-for-ruby-on-rails/</link>
		<comments>http://www.kbedell.com/2011/03/15/seed-data-versus-testing-data-and-custom-rake-tasks-for-ruby-on-rails/#comments</comments>
		<pubDate>Tue, 15 Mar 2011 19:57:20 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[agile]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[seed_data]]></category>
		<category><![CDATA[test_data]]></category>

		<guid isPermaLink="false">http://www.kbedell.com/?p=296</guid>
		<description><![CDATA[I&#8217;ve been a part of discussions on how to define &#8216;seed&#8217; data versus &#8216;testing&#8217; data on a Ruby on Rails project. Here&#8217;s a summary of what I&#8217;ve seen work well &#8212; please comment if you have anything to add. Data loading is done for 2 purposes: First, to load data that will seed the system [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been a part of discussions on how to define &#8216;seed&#8217; data versus &#8216;testing&#8217; data on a Ruby on Rails project. Here&#8217;s a summary of what I&#8217;ve seen work well &#8212; please comment if you have anything to add.</p>
<p>Data loading is done for 2 purposes: First, to load data that will seed the system for ongoing use and that will live in the production environment, and second, to use for system testing. These two types of data are stored in two different ways.</p>
<h3>Loading &#8216;seed&#8217; data that will be required in production.</h3>
<p>This data should be defined in <code>/db/seeds.rb</code> and loaded using the <code>rake db:seed</code> command. It should be loaded one time after initial database creation. If changes to the seeds.rb file are made, the database should be rebuilt and reloaded from scratch.</p>
<h3>Loading test data.</h3>
<p>This is data that is required for testing the application. Specifically, it is data that should not appear in the production application. </p>
<p>This data should be loaded through model classes (or using factory_girl or similar) in a &#8216;rake&#8217; file, for example <code>/lib/tasks/test_data.rake</code> and then load it using the rake task <code>rake app:load_demo_data</code>. Similar to loading seeds.rb, if the test data changes then the database should be reloaded from scratch.</p>
<p>(Note that here the rake file is named <code>/lib/tasks/test_data.rake</code> and the rake task to load the data is <code>rake app:load_demo_data</code>. This would vary based on what you felt was an appropriate name for your project.)</p>
<h3>Overall database loading/unloading</h3>
<p>The correct way to load data into the database is to use the following rake commands:</p>
<p>  <code>rake db:drop db:create db:migrate db:seed app:load_demo_data</code></p>
<p>As you see, multiple rake commands can be run one after another sequenced on the same line. </p>
<p>This should be able to be done at any time by any person in development. If you make a change to any data, please assume that the above sequence of rake commands could be run at any time and all data could be replaced. </p>
<p>Note also that the above command will load data into the &#8216;development&#8217; instance of your database. If you need to load (or reload) the production or test environments, then precede this command with the appropriate RAILS_ENV like so:</p>
<p>  <code>RAILS_ENV=test rake db:drop db:create db:migrate db:seed app:load_demo_data</code></p>
<p>Here&#8217;s an example of what the rake file <code>/lib/tasks/test_data.rake</code> might look like:</p>
<p><code><br />
namespace :app do<br />
&nbsp;&nbsp;desc <<-DESC<br />
&nbsp;&nbsp;&nbsp;&nbsp;Load testing data.<br />
&nbsp;&nbsp;&nbsp;&nbsp;Run using the command 'rake app:load_demo_data'<br />
&nbsp;&nbsp;DESC<br />
&nbsp;&nbsp;task :load_demo_data => [:environment] do</p>
<p>&nbsp;&nbsp;# Only data not required in production should be here.<br />
&nbsp;&nbsp;# If it needs to be there in production, it belongs in seeds.rb.</p>
<p>&nbsp;&nbsp;User.delete_all<br />
&nbsp;&nbsp;Category.delete_all</p>
<p>&nbsp;&nbsp;User.create(:login => "test_user",  :password => "testpass")<br />
&nbsp;&nbsp;Category.create(:category => 'Category #1')</p>
<p>&nbsp;&nbsp;# Other test data should be added here...</p>
<p>&nbsp;&nbsp;end<br />
end<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kbedell.com/2011/03/15/seed-data-versus-testing-data-and-custom-rake-tasks-for-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A simple example of automating the creation of named_scopes in rails</title>
		<link>http://www.kbedell.com/2010/12/02/simple_example_of_automating_creation_of_named_scopes_rails/</link>
		<comments>http://www.kbedell.com/2010/12/02/simple_example_of_automating_creation_of_named_scopes_rails/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 04:34:59 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[metaprogamming]]></category>
		<category><![CDATA[named_scope]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kbedell.com/?p=213</guid>
		<description><![CDATA[Here&#8217;s a common scenario: You have an attribute that can be a set of specific values, like the status of a transaction that can be &#8216;processing&#8217;, &#8216;succeeded&#8217; or &#8216;failed&#8217;.  You now want to make it easy to check these values on your model object and you think you might want to create a named scope [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a common scenario:</p>
<p>You have an attribute that can be a set of specific values, like the status of a transaction that can be &#8216;processing&#8217;, &#8216;succeeded&#8217; or &#8216;failed&#8217;.  You now want to make it easy to check these values on your model object and you think you might want to create a named scope for one of more of them.</p>
<p>Here&#8217;s a simple &#8216;design pattern&#8217; that makes this easy and fast.</p>
<p>First, define a Module within your Class and use it to define constants for the allowed values, then put those values in an Array. This tightens up the code by encouraging developers to only set that attribute to one of the &#8216;predefined&#8217; values. For example:</p>
<pre>
class Transaction &lt; ActiveRecord::Base

  # Allowed values for attribute 'status'
  module Status
    PROCESSING = 'processing'
    SUCCEEDED   = 'succeeded'
    FAILED     = 'failed'
  end
  STATUSES = [Status::PROCESSING, Status::SUCCEEDED, Status::FAILED]

end
</pre>
<p>This is a simple &#8216;design pattern&#8217; that encourages the use of good practices in development.</p>
<p>Now that we&#8217;ve got this in place, let&#8217;s spin up some ruby magic to automate the creation of status methods and named scopes for each of the different values. We&#8217;ll do so with this bit of code:</p>
<pre>
STATUSES.each do |status|
   # Define a 'setter' method for each status value
   define_method "#{status}!".to_sym do
     self.status = status
   end
   # Define an 'interrogator' method for each status value
   define_method "#{status}?".to_sym do
     self.status == status
   end
  # Define a named scope for each status value
   self.named_scope status.to_sym, :conditions =&gt; { :status =&gt; status }
 end
</pre>
<p>It&#8217;s part of the magic of Ruby that classes can so easily extend themselves by adding new methods. </p>
<p>And if you change the allowed values for the &#8216;status&#8217; field, simply modify the Module definition and add the new value to the array &#8212; creation of named scopes and the other methods will be taken care of automatically.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kbedell.com/2010/12/02/simple_example_of_automating_creation_of_named_scopes_rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get the contents of the database.yml file from ActiveRecord</title>
		<link>http://www.kbedell.com/2009/03/06/how-to-get-the-contents-of-the-database-ml-file-from-activerecord-and-connect-to-two-databases-at-once/</link>
		<comments>http://www.kbedell.com/2009/03/06/how-to-get-the-contents-of-the-database-ml-file-from-activerecord-and-connect-to-two-databases-at-once/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 14:09:17 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kbedell.com/?p=70</guid>
		<description><![CDATA[Save to delicious. Sometimes when using Active Record you may want to create a database connection to a database other than the default database specified in your database.yml file. When this happens, the easiest way to do it is to use the Class-level convenience method on ActiveRecord::Base. Like this: Imagine your database.yml entry looks like [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://delicious.com/url/c5db65f90af6455a805484b6e7c67ccf">Save to delicious.</a></p>
<p>Sometimes when using Active Record you may want to create a database connection to a database other than the default database specified in your database.yml file.</p>
<p>When this happens, the easiest way to do it is to use the Class-level convenience method on ActiveRecord::Base. Like this:</p>
<p>Imagine your database.yml entry looks like this:</p>
<pre>development_foo:
  adapter: mysql
  encoding: utf8
  database: foo
  pool: 5
  username: my_user
  password: my_pass
  socket: /tmp/mysql.sock</pre>
<p>Now you want to create active record models that use that database connection. Here&#8217;s how:</p>
<pre># 'Bar' model points to the table 'bars' in the database 'foo'

class Bar &lt; ActiveRecord::Base

  # specify the database.yml entry
  $db_config = 'development_foo'

  # Fetch the database.yml configuration
  $config = ActiveRecord::Base.configurations[$db_config]

 # Now we can establish a connection to that database and
 # this ActiveRecord model class will point to that database.
  establish_connection $config

end</pre>
<p>It&#8217;s simple!</p>
<h6>Copyright Kevin Bedell, 2009<br />
All this code can be freely used under the terms of the MIT License.</h6>
]]></content:encoded>
			<wfw:commentRss>http://www.kbedell.com/2009/03/06/how-to-get-the-contents-of-the-database-ml-file-from-activerecord-and-connect-to-two-databases-at-once/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

