<?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>Sean&#039;s Blog &#187; active record</title>
	<atom:link href="http://seanbehan.com/tag/active-record/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanbehan.com</link>
	<description>Web Programming, Ruby on Rails, Wordpress, PHP from Burlington, Vermont</description>
	<lastBuildDate>Wed, 18 Jan 2012 21:44:54 +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>Rails Find All by Birthday: How to Find Upcoming Birthdays with ActiveRecord</title>
		<link>http://seanbehan.com/ruby-on-rails/rails-find-all-by-birthday-how-to-find-upcoming-birthdays-with-activerecord/</link>
		<comments>http://seanbehan.com/ruby-on-rails/rails-find-all-by-birthday-how-to-find-upcoming-birthdays-with-activerecord/#comments</comments>
		<pubDate>Sun, 12 Jun 2011 21:34:02 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[birthdays]]></category>
		<category><![CDATA[cache counter]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[scopes]]></category>
		<category><![CDATA[timestamp]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=1355</guid>
		<description><![CDATA[There are a few ways to solve this problem. However, I think the easiest is to cache the day of the year that the user is born on as an integer. If stored alongside the timestamp we can quickly get a list but properly handle the full birthday elsewhere, such as in the view. You [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Frails-find-all-by-birthday-how-to-find-upcoming-birthdays-with-activerecord%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Rails Find All by Birthday: How to Find Upcoming Birthdays with ActiveRecord" data-url="http://seanbehan.com/ruby-on-rails/rails-find-all-by-birthday-how-to-find-upcoming-birthdays-with-activerecord/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>There are a few ways to solve this problem. However, I think the easiest is to cache the day of the year that the user is born on as an integer. If stored alongside the timestamp we can quickly get a list but properly handle the full birthday elsewhere, such as in the view. You don&#8217;t want to rely on just the cached day of year because leap year is not accounted for. </p>
<p>The model will need both born_at and birthday columns. </p>
<pre class="wp-code-highlight prettyprint">
create_table :users do |t|
  t.timestamp :born_at  # full timestamp
  t.integer :birthday 	# just the day of year
end
</pre>
<p>The user model also needs a callback (before_save) to set and or update the cached birthday column based on the full timestamp. For convenience, a named scope can be added to the model which will let you call User.birthdays. </p>
<pre class="wp-code-highlight prettyprint">
class User
  # User.birthdays
  scope :birthdays, lambda { where('birthday in(?)', 7.times.map{|i| Time.now.yday + i}) }

  def before_save
  	self.birthday = born_at.yday
  end
end
</pre>
<p>You could also use the week in year (1 &#8211; 52) for the cache. Using the day you can look an arbitrary number of days ahead.</p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/rails-find-all-by-birthday-how-to-find-upcoming-birthdays-with-activerecord/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reusing Scopes (Formerly Named_scope) In Rails 3</title>
		<link>http://seanbehan.com/ruby-on-rails/reusing-scopes-formerly-named_scope-in-rails-3/</link>
		<comments>http://seanbehan.com/ruby-on-rails/reusing-scopes-formerly-named_scope-in-rails-3/#comments</comments>
		<pubDate>Thu, 05 May 2011 13:22:03 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[named scopes]]></category>
		<category><![CDATA[scopes]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=1296</guid>
		<description><![CDATA[You can easily chain scopes together in your models. class Article < ActiveRecord::Base scope :ordered, order('position ASC') scope :published, ordered.where('published = ?', true) scope :for_homepage, published.limit(3) end Article.for_homepage.to_sql # => SELECT \"articles\".* FROM \"articles\" WHERE (published = 't') ORDER BY position LIMIT 3]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Freusing-scopes-formerly-named_scope-in-rails-3%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Reusing Scopes (Formerly Named_scope) In Rails 3" data-url="http://seanbehan.com/ruby-on-rails/reusing-scopes-formerly-named_scope-in-rails-3/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>You can easily chain scopes together in your models.<br />
<code><br />
class Article < ActiveRecord::Base<br />
  scope :ordered, order('position ASC')<br />
  scope :published, ordered.where('published = ?', true)<br />
  scope :for_homepage, published.limit(3)<br />
end<br />
</code></p>
<p><code><br />
Article.for_homepage.to_sql<br />
# => SELECT \"articles\".* FROM \"articles\" WHERE (published = 't') ORDER BY position LIMIT 3<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/reusing-scopes-formerly-named_scope-in-rails-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Select Distinct in Rails with Active Record</title>
		<link>http://seanbehan.com/ruby-on-rails/select-distinct-in-rails-with-active-record/</link>
		<comments>http://seanbehan.com/ruby-on-rails/select-distinct-in-rails-with-active-record/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 23:23:29 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[distinct]]></category>
		<category><![CDATA[select]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=1153</guid>
		<description><![CDATA[User.find :all, :select =&#62; &#34;DISTINCT occupation&#34;]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Fselect-distinct-in-rails-with-active-record%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Select Distinct in Rails with Active Record" data-url="http://seanbehan.com/ruby-on-rails/select-distinct-in-rails-with-active-record/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><pre class="wp-code-highlight prettyprint">
User.find :all, :select =&gt; &quot;DISTINCT occupation&quot;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/select-distinct-in-rails-with-active-record/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Load All ActiveRecord::Base Model Classes in Rails Application</title>
		<link>http://seanbehan.com/programming/load-all-activerecordbase-model-classes-in-rails-application/</link>
		<comments>http://seanbehan.com/programming/load-all-activerecordbase-model-classes-in-rails-application/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 16:26:38 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[classify]]></category>
		<category><![CDATA[constants]]></category>
		<category><![CDATA[initialize]]></category>
		<category><![CDATA[load]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[rake]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=912</guid>
		<description><![CDATA[Here is a simple rake task which will instantiate all of your Active Record models, provided that they are located in the RAILS_ROOT/app/models directory. Interestingly, all plugin models are instantiated by default when you run the task, for instance, if you are using the Acts As Taggable On plugin, you have access to Tag, Tagging [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fprogramming%2Fload-all-activerecordbase-model-classes-in-rails-application%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Load All ActiveRecord::Base Model Classes in Rails Application" data-url="http://seanbehan.com/programming/load-all-activerecordbase-model-classes-in-rails-application/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>Here is a simple rake task which will instantiate all of your Active Record models, provided that they are located in the RAILS_ROOT/app/models directory. Interestingly, all plugin models are instantiated by default when you run the task, for instance, if you are using the Acts As Taggable On plugin, you have access to Tag, Tagging without having to include the plugin models directory path to the task.  </p>
<pre class="wp-code-highlight prettyprint">
namespace :load_ar do
    desc &quot;load up all active record models&quot;
    task :models =&amp;gt; :environment do
      models = ActiveRecord::Base.send(:subclasses)
      Dir[&quot;#{RAILS_ROOT}/app/models/*&quot;].each do |file|
         model = File.basename(file, &quot;.*&quot;).classify
         models &amp;lt;&amp;lt; model unless models.include?(model)
      end
   end
end
</pre>
<p>If your&#8217;re in the console, you can get all the load paths for your Active Record models with the following from the API. </p>
<pre class="wp-code-highlight prettyprint">
Rails.configuration.load_paths.each do |path|
   p path
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/programming/load-all-activerecordbase-model-classes-in-rails-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rake DB Everything, Dump, Destroy, Create, Load</title>
		<link>http://seanbehan.com/ruby-on-rails/rake-db-everything-dump-destroy-create-load/</link>
		<comments>http://seanbehan.com/ruby-on-rails/rake-db-everything-dump-destroy-create-load/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 22:01:10 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[tasks]]></category>
		<category><![CDATA[yaml]]></category>
		<category><![CDATA[yaml_db]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=916</guid>
		<description><![CDATA[I&#8217;m a big fan of the yaml_db plugin. But I don&#8217;t like running rake db:data:load, only to find that my db columns mismatch my model attributes, thus aborting the data import task. To quickly add/remove columns/attributes from a model and rebuild the database with previous db info, I wrote this simple rake task. It saves [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Frake-db-everything-dump-destroy-create-load%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Rake DB Everything, Dump, Destroy, Create, Load" data-url="http://seanbehan.com/ruby-on-rails/rake-db-everything-dump-destroy-create-load/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>I&#8217;m a big fan of the yaml_db plugin. But I don&#8217;t like running rake db:data:load, only to find that my db columns mismatch my model attributes, thus aborting the data import task.  To quickly add/remove columns/attributes from a model and rebuild the database with previous db info, I wrote this simple rake task. It saves model records as yaml in db/seandb.yml, and reloads them in the same task but with the db rebuilt. I nest the save method in begin/rescue so that if there are any conflicts the task will continue.</p>
<p>This biggest challenge was to get a list of all the active record models in the Rails app. This problem has been posted a lot and I didn&#8217;t find an easy solution. For now, I just look in the app/models directory and &#8216;classify&#8217; and &#8216;constantize&#8217;  the file name. This will load the model so that I can iterate over all the active record subclasses and call the appropriate Model.find(:all) method. I could maybe do the same w/ all files in the Rails.configuration.load_paths, but my models are, at least for now, under app/models. Plugin models are loaded and available from the rake task if you require the :environment.</p>
<p>It&#8217;s as simple as running</p>
<pre class="wp-code-highlight prettyprint">
rake db:everything
</pre>
<pre class="wp-code-highlight prettyprint">
SAVEDB = &quot;#{RAILS_ROOT}/db/seandb.yml&quot;
namespace :db do
   task :everything =&gt; [:environment, :spit, :drop, :create, :migrate, :populate] do
     desc &quot;spit out model records as yaml, rebuild the database and repopulate the db&quot;
   end

   task :spit do
    Dir[&quot;#{RAILS_ROOT}/app/models/*&quot;].each {|file| (File.basename(file,&quot;.*&quot;).classify.constantize)}
    File.open(SAVEDB, &quot;w&quot;) do |f|
      ActiveRecord::Base.send(:subclasses).each do |model|
        begin
          model.find(:all).each do |record|
              f.write(record.to_yaml)
              print &quot;\twrote #{record.class}\n&quot;
          end
        rescue
        end
      end
    end
   end

   task :populate do
    File.open( SAVEDB ) do |yf|
      YAML.each_document( yf ) do |ydoc|
        begin
          m = ydoc.clone
          m.save
          p &quot;...saved #{m.id}&quot;
        rescue
        end
      end
    end
   end
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/rake-db-everything-dump-destroy-create-load/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Active Record Find Methods</title>
		<link>http://seanbehan.com/ruby-on-rails/active-record-find-methods/</link>
		<comments>http://seanbehan.com/ruby-on-rails/active-record-find-methods/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 20:46:03 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=883</guid>
		<description><![CDATA[Active Record find methods for selecting range from http://charlesmaxwood.com/notes-from-reading-activerecordbase/ Student.find(:all, :conditions =&#62; { :grade =&#62; 9..12 }) return a range Student.find(:all, :conditions =&#62; { :grade =&#62; [9,11,12] }) will return an &#34;in()&#34;]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Factive-record-find-methods%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Active Record Find Methods" data-url="http://seanbehan.com/ruby-on-rails/active-record-find-methods/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>Active Record find methods for selecting range from <a href="http://charlesmaxwood.com/notes-from-reading-activerecordbase/">http://charlesmaxwood.com/notes-from-reading-activerecordbase/</a></p>
<pre class="wp-code-highlight prettyprint">
Student.find(:all, :conditions =&gt; { :grade =&gt; 9..12 })
return a range
Student.find(:all, :conditions =&gt; { :grade =&gt; [9,11,12] })
will return an &quot;in()&quot;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/active-record-find-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Change database column names for form validations in Rails</title>
		<link>http://seanbehan.com/ruby-on-rails/change-database-column-names-for-form-validations-in-rails/</link>
		<comments>http://seanbehan.com/ruby-on-rails/change-database-column-names-for-form-validations-in-rails/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 21:39:56 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[override]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[validations]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=888</guid>
		<description><![CDATA[When you use validations in Rails, db column names are used as &#8216;keys&#8217; for error messages. This is usually the preferred way to go about it because this maps nicely to the form fields. However, if you use a virtual attribute this may not be the case. For example, I have a &#8216;password_crypted&#8217; field in [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Fchange-database-column-names-for-form-validations-in-rails%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Change database column names for form validations in Rails" data-url="http://seanbehan.com/ruby-on-rails/change-database-column-names-for-form-validations-in-rails/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>When you use validations in Rails, db column names are used as &#8216;keys&#8217; for error messages. This is usually the preferred way to go about it because this maps nicely to the form fields. However, if you use a virtual attribute this may not be the case. For example, I have a &#8216;password_crypted&#8217; field in my users table that I don&#8217;t want my user to see if they fail to complete the field. Instead of returning &#8220;Password crypted cannot be blank&#8221; I just want to tell them that a password can&#8217;t be blank. If you provide a custom &#8216;:message&#8217; on the validation this won&#8217;t replace the column name. The solution is to override the &#8220;human_attribute_name&#8221; class method and map specific column names to the string you want to use instead.</p>
<pre class="wp-code-highlight prettyprint">
class User &amp;lt; ActiveRecord::Base
  validates_presence_of :password_crypted
  ATTR_NAMES = {:password_crypted =&gt; &quot;Password&quot;}
  def self.human_attribute_name(attr)
     ATTR_NAMES[attr.to_sym] || super
  end
end
</pre>
<p>I found these resources helpful while I was in search for a solution to this problem. <a href="http://stackoverflow.com/questions/808547/fully-custom-validation-error-message-with-rails"></p>
<p>http://stackoverflow.com/questions/808547/fully-custom-validation-error-message-with-rails</a></p>
<p><a href="http://henrik.nyh.se/2007/12/change-displayed-column-name-in-rails-validation-messages"></p>
<p>http://henrik.nyh.se/2007/12/change-displayed-column-name-in-rails-validation-messages</a></p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/change-database-column-names-for-form-validations-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails Plugin Acts as Taggable on Steriods</title>
		<link>http://seanbehan.com/ruby-on-rails/rails-plugin-acts-as-taggable-on-steriods/</link>
		<comments>http://seanbehan.com/ruby-on-rails/rails-plugin-acts-as-taggable-on-steriods/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 22:46:12 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[migration]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[tags]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=831</guid>
		<description><![CDATA[You can download it here http://github.com/suitmymind/acts-as-taggable-on-steroids as well as read usage info (which is for the most part reprinted here). ./script/plugin install http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids ./script/generate acts_as_taggable_migration rake db:migrate Then in your model class Post &#60; ActiveRecord::Base acts_as_taggable end And usage is as follows p = Post.find(:first) p.tag_list # [] p.tag_list = &#34;Funny, Silly&#34; p.save p.tag_list # [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Frails-plugin-acts-as-taggable-on-steriods%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Rails Plugin Acts as Taggable on Steriods" data-url="http://seanbehan.com/ruby-on-rails/rails-plugin-acts-as-taggable-on-steriods/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>You can download it here <a href="http://github.com/suitmymind/acts-as-taggable-on-steroids">http://github.com/suitmymind/acts-as-taggable-on-steroids</a> as well as read usage info (which is for the most part reprinted here).</p>
<pre class="wp-code-highlight prettyprint">
./script/plugin install http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids
./script/generate acts_as_taggable_migration
rake db:migrate
</pre>
<p>Then in your model</p>
<pre class="wp-code-highlight prettyprint">
class Post &lt; ActiveRecord::Base
    acts_as_taggable
 end
</pre>
<p>And usage is as follows</p>
<pre class="wp-code-highlight prettyprint">
p = Post.find(:first)
p.tag_list # []
p.tag_list = &quot;Funny, Silly&quot;
p.save
p.tag_list # [&quot;Funny&quot;, &quot;Silly&quot;]
p.tag_list.add(&quot;Great&quot;, &quot;Awful&quot;)
p.tag_list.remove(&quot;Funny&quot;)

#to find...
Post.find_tagged_with('Funny, Silly')
Post.find_tagged_with('Funny, Silly', :match_all =&gt; true)
</pre>
<p>To use this in a form and let users enter a comma separated list of tag names&#8230;</p>
<pre class="wp-code-highlight prettyprint">
form_for @post do |f|
f.text_field :tag_list
</pre>
<p>And to get a tag cloud</p>
<pre class="wp-code-highlight prettyprint">
  #controller
  class PostController &amp;lt; ApplicationController
    def tag_cloud
      @tags = Post.tag_counts
    end
  end

  # and in view...
  &amp;lt;style&amp;gt;
  .css1 { font-size: 1.0em; }
  .css2 { font-size: 1.2em; }
  .css3 { font-size: 1.4em; }
  .css4 { font-size: 1.6em; }
  &amp;lt;/style&amp;gt;

  &amp;lt;% tag_cloud @tags, %w(css1 css2 css3 css4) do |tag, css_class| %&amp;gt;
    &amp;lt;%= link_to tag.name, { :action =&amp;gt; :tag, :id =&amp;gt; tag.name }, :class =&amp;gt; css_class %&amp;gt;
  &amp;lt;% end %&amp;gt;
</pre>
<p>*Note. If you have a controller &#8220;tags_controller.rb&#8221; and the auto generated (if you used ./script/generate) helper file &#8220;tags_helper.rb&#8221; you&#8217;ll need to make sure to copy the contents of the plugin lib module of the same name, into the helper file. You&#8217;ll get an error otherwise.</p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/rails-plugin-acts-as-taggable-on-steriods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Descending Sort By in Model For Active Record Hash on Created_at attribute</title>
		<link>http://seanbehan.com/ruby-on-rails/descending-sort-by-in-model-for-active-record-hash-on-created_at-attribute/</link>
		<comments>http://seanbehan.com/ruby-on-rails/descending-sort-by-in-model-for-active-record-hash-on-created_at-attribute/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 19:08:48 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[created_at]]></category>
		<category><![CDATA[sort_by]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=614</guid>
		<description><![CDATA[If you have a couple collections from the database and you want to sort it without the help of Active Record, take a look at the sort_by method on Array type. I&#8217;ve used this before when I have a couple of collections which are slightly different but I need them in a chronological order. @posts_group_a [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Fdescending-sort-by-in-model-for-active-record-hash-on-created_at-attribute%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Descending Sort By in Model For Active Record Hash on Created_at attribute" data-url="http://seanbehan.com/ruby-on-rails/descending-sort-by-in-model-for-active-record-hash-on-created_at-attribute/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>If you have a couple collections from the database and you want to sort it without the help of Active Record, take a look at the sort_by method on Array type. I&#8217;ve used this before when I have a couple of collections which are slightly different but I need them in a chronological order.</p>
<pre class="wp-code-highlight prettyprint">

  @posts_group_a = Post.find :all, :conditions =&gt; [&quot;user_id = ?&quot;, current_user.id]
  @posts_group_b = Post.find :all, :conditions =&gt; [&quot;user_id = ?&quot;, friend_user.id]

  #merge the two arrays here
  @posts = @posts_group_a + @posts_group_b

  # notice the &quot;-&quot; is for descending order and the &quot;to_i&quot; casts the date time to an integer (required)
  @posts.sort_by {|post| - post.created_at.to_i}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/descending-sort-by-in-model-for-active-record-hash-on-created_at-attribute/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Output Logger and SQL to the Rails Console in Development Mode</title>
		<link>http://seanbehan.com/ruby-on-rails/output-logger-and-sql-to-the-rails-console-in-development-mode/</link>
		<comments>http://seanbehan.com/ruby-on-rails/output-logger-and-sql-to-the-rails-console-in-development-mode/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 18:22:11 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[stdout]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=612</guid>
		<description><![CDATA[If you want to take a look at the SQL being generated by active record while your using the console, you can either type this into the console when it loads ActiveRecord::Base.logger = Logger.new(STDOUT) Or you can add it to your environment so that it&#8217;ll be the default behavior rails_root/config/environments/development.rb #... ActiveRecord::Base.logger = Logger.new(STDOUT) It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Foutput-logger-and-sql-to-the-rails-console-in-development-mode%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Output Logger and SQL to the Rails Console in Development Mode" data-url="http://seanbehan.com/ruby-on-rails/output-logger-and-sql-to-the-rails-console-in-development-mode/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>If you want to take a look at the SQL being generated by active record while your using the console, you can either type this into the console when it loads</p>
<pre class="wp-code-highlight prettyprint">
ActiveRecord::Base.logger = Logger.new(STDOUT)
</pre>
<p>Or you can add it to your environment so that it&#8217;ll be the default behavior<br />
rails_root/config/environments/development.rb</p>
<pre class="wp-code-highlight prettyprint">
#...
ActiveRecord::Base.logger = Logger.new(STDOUT)
</pre>
<p>It&#8217;s a nice way to keep you away of any expensive queries you may unknowingly be writing!</p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/output-logger-and-sql-to-the-rails-console-in-development-mode/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

