<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Bandwagon</title>
	<atom:link href="http://bandwagonblog.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://bandwagonblog.wordpress.com</link>
	<description>Software is a passion.  Not a job.</description>
	<lastBuildDate>Mon, 09 Feb 2009 02:13:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='bandwagonblog.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/cd238b41d79a77078f837001ad6bc313?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Bandwagon</title>
		<link>http://bandwagonblog.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://bandwagonblog.wordpress.com/osd.xml" title="Bandwagon" />
	<atom:link rel='hub' href='http://bandwagonblog.wordpress.com/?pushpress=hub'/>
		<item>
		<title>ActiveRecord Validations</title>
		<link>http://bandwagonblog.wordpress.com/2009/02/08/activerecord-validations/</link>
		<comments>http://bandwagonblog.wordpress.com/2009/02/08/activerecord-validations/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 02:12:55 +0000</pubDate>
		<dc:creator>bandwagonblog</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[validations]]></category>

		<guid isPermaLink="false">http://bandwagonblog.wordpress.com/?p=486</guid>
		<description><![CDATA[The API provided for ActiveRecord validations gives developers a way to describe valid model object state.  The validations hook into the object life cycle and inspect object state to determine the validity of attributes.  If any validations fail, they will be reported as errors in the model, which are accessible through the errors attribute, an [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=486&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The API provided for ActiveRecord validations gives developers a way to describe valid model object state.  The validations hook into the object life cycle and inspect object state to determine the validity of attributes.  If any validations fail, they will be reported as errors in the model, which are accessible through the errors attribute, an instance of ActiveRecord::Errors.  If there are no errors on an ActiveRecord model, the <em>errors</em> collection is empty, and the <em>valid?</em> method would return true.  The validations you can use will appear like declarations; they are simply class methods.  Each validation method accepts n attributes, along with an optional set of options.  Any <em>bang!</em> methods that are called on the model that fail will raise an error with the description of the failures.</p>
<p><strong>validates_acceptance_of</strong> :<em>some_attribute</em> # attribute must be accepted.<br />
<strong>validates_acceptance_of</strong> :<em>some_attribute</em>, :accept=&gt;&#8217;YES&#8217; # considered acceptance. default=1</p>
<p><strong>validates_associated</strong> :<em>has_many_association</em> # the valid? method will be called on each object</p>
<p># creates a virtual <em>attribute</em>_confirmation &#8211; gets compared to attribute.<br />
<strong>validates_confirmation_of</strong> :<em>attribute </em># the user interface must use attribute_confirmation</p>
<p><strong><br />
validates_each</strong> :attribute_one, :attribute_two, :attribute_etc do |record, attr, value|<br />
# you can give <em>validates_each</em> an array of attributes and a block to perform.<br />
# The block marks the object as valid or not by adding to the errors array or not.<br />
# example&#8230;. record.errors.add(attr) unless PurchasingSystem.validate(attr,value)<br />
end<br />
<strong>validates_inclusion_of</strong> :gender, :in=&gt;['m','f'], :message =&gt; &#8216;Enter m or f.&#8217;<br />
<strong>validates_exclusion_of</strong> :login, :in=&gt;['admin', 'root', 'superuser'], :message=&gt;&#8217;No no no.&#8217;<br />
# Available as a plugin&#8230; optional :allow_nil =&gt; false<br />
<strong>validates_existence_of</strong> :attribute # checks foreign key in belongs_to refrences existing record<br />
# script/plugin install http://svn.hasmanythrough.com/public/plugins/validates_existence<br />
<strong>validates_format_of</strong> :email, :with=&gt; &#8230; #some regular expression</p>
<p># provides optional :wrong_length, :too_long, and :too_short options<br />
<strong>validates_length_of </strong>:login, :minimum =&gt; 5  # minimum value<br />
<strong>validates_length_of</strong> :login, :within =&gt; 5..20  # a Ruby Range<br />
<strong>validates_length_of</strong> :account_number, :is =&gt; 16  # exact value<br />
<strong>validates_length_of</strong> :account_number, :is =&gt; 16, :wrong_length =&gt; &#8220;should be %d characters.&#8221;</p>
<p><strong>validates_numericality_of</strong> :some_numeric_attribute, :integer_only =&gt; true</p>
<p><strong>validates_presence_of </strong>:some_attr_checked_if_blank # won&#8217;t allow nil or &#8220;&#8221;</p>
<p><strong>validates_uniqueness_of </strong>:attribute_is_unique_for_all_models_of_same_type_via_query<br />
<strong>validates_uniqueness_of</strong> :line_two, :scope =&gt; [:line_one, :city, :zip]<br />
<strong>validates_uniqueness_of</strong> :login_name, :case_sensitive =&gt; false</p>
<p><span style="text-decoration:underline;"><strong>COMMON VALIDATION OPTIONS</strong></span></p>
<p>:allow_nil =&gt; true # skips validation if the value of the attribute is nil<br />
:if =&gt; &#8230; # conditional validation<br />
:message =&gt; &#8230; #specific message describing the validation failure, overriding the default msg<br />
: on =&gt; &#8230; # by default, validations are run on save.  specify create for validates_uniqueness_of</p>
<p><span style="text-decoration:underline;"><strong>THE ERRORS COLLECTION<br />
</strong></span></p>
<p>Errors for attribute validations will be created by prepending the canned validation messages with the name of the associated model.</p>
<p><strong>add_to_base</strong>(msg) # adds complete msg to the object state for self; not any particular attribute<br />
<strong>add</strong>(attribute,msg) # adds partial msg to a particular attribute<br />
<strong>clear</strong> # clears the errors collection<br />
<strong>invalid?</strong>(attribute) # true or false depending on if validation errors are associated<br />
<strong>on(attribute)</strong> # nil,string or array depending on the errors collection state for the attribute</p>
<p><span style="text-decoration:underline;"><strong>CUSTOM VALIDATION</strong></span> &#8211; Rails provides three hook methods for custom validation</p>
<p><strong>validate</strong> # called after standard Rails validations<br />
<strong>validate_on_create</strong> # called if new_record?<br />
<strong>validate_on_update</strong> # called if not a new_record?</p>
<p><span style="text-decoration:underline;"><strong>SKIPPING VALIDATION</strong></span></p>
<p>The ActiveRecord::Validations module mixed into ActiveRecord::Base affects three instance methods; save, save! and update_attribute are affected.  Validations can be skipped by passing in false as a method parameter.  As a result of ActiveRecord::Validations calling :alias_method_chain :save, :validation &#8230; you end up with save_without_validations.  Be careful &#8212; update_attributes does use validations, while the singular form, update_attribute, does not.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bandwagonblog.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bandwagonblog.wordpress.com/486/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bandwagonblog.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bandwagonblog.wordpress.com/486/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bandwagonblog.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bandwagonblog.wordpress.com/486/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bandwagonblog.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bandwagonblog.wordpress.com/486/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bandwagonblog.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bandwagonblog.wordpress.com/486/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bandwagonblog.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bandwagonblog.wordpress.com/486/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bandwagonblog.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bandwagonblog.wordpress.com/486/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=486&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bandwagonblog.wordpress.com/2009/02/08/activerecord-validations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67d14776ca14b159cf32a7967067b7fe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bandwagonblog</media:title>
		</media:content>
	</item>
		<item>
		<title>ActiveRecord one-to-one Relationships</title>
		<link>http://bandwagonblog.wordpress.com/2009/02/01/activerecord-one-to-one-relationships/</link>
		<comments>http://bandwagonblog.wordpress.com/2009/02/01/activerecord-one-to-one-relationships/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 18:11:20 +0000</pubDate>
		<dc:creator>bandwagonblog</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[one-to-one]]></category>

		<guid isPermaLink="false">http://bandwagonblog.wordpress.com/?p=482</guid>
		<description><![CDATA[has_one is used to declare one-to-one relationships (along with the belongs_to method). Call belongs_to on the model that has the foreign key column in the database.  Use has_one in the other model.  LIMIT 1 clause is appended to the generated SQL that activerecord uses to find the relationship. Examples :has_one :last_timesheet :has_one :primary_account :has_one :profile_photo [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=482&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong><span style="text-decoration:underline;">has_one</span></strong> is used to declare one-to-one relationships (along with the belongs_to method).<br />
Call belongs_to on the model that has the foreign key column in the database.  Use has_one in the other model.  LIMIT 1 clause is appended to the generated SQL that activerecord uses to find the relationship.</p>
<p><span style="text-decoration:underline;">Examples</span><br />
:has_one :last_timesheet<br />
:has_one :primary_account<br />
:has_one :profile_photo</p>
<p>class Avatar &lt; ActiveRecord::Base; belongs_to :user; end<br />
class User &lt; ActiveRecord::Base; has_one :avatar; end<br />
user = User.find(:first)<br />
user.avatar # =&gt; nil<br />
user.build_avatar(:url =&gt; &#8216;/avatars/smiling&#8217;)<br />
user.avatar.save # =&gt; true</p>
<p><em>Specifying :dependent =&gt; :destroy will cause records no longer associated with the parent model to be destroyed.</em></p>
<p>class User &lt; ActiveRecord::Base<br />
has_one :avatar, :dependent =&gt; :destroy<br />
end</p>
<p><span style="text-decoration:underline;"><strong>has_one Options</strong></span></p>
<p><strong>:as =&gt; </strong>sets up a polymorphic association</p>
<p><strong>:class_name =&gt; </strong>specify the class this association uses (useful when not using conventions)</p>
<p><strong>:conditions =&gt; </strong>specify attributes the object must meet to be included in the association</p>
<p><strong>:dependent =&gt; </strong>specifies how ActiveRecord should treat associated objects when the parent object is deleted.  There are a few different values that you can pass and they work just like the :dependent option of has_many.  Default is :nullify, :delete won&#8217;t use callbacks and :destroy will use callbacks.</p>
<p><strong>:foreign_key =&gt; </strong>specifies the name of the fk column on the table of the association</p>
<p><strong>:include =&gt; </strong>eagerloading purposes</p>
<p><strong> <img src='http://s1.wp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder =&gt; </strong>sql fragment used to order the results (used in the ORDER BY clause)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bandwagonblog.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bandwagonblog.wordpress.com/482/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bandwagonblog.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bandwagonblog.wordpress.com/482/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bandwagonblog.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bandwagonblog.wordpress.com/482/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bandwagonblog.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bandwagonblog.wordpress.com/482/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bandwagonblog.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bandwagonblog.wordpress.com/482/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bandwagonblog.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bandwagonblog.wordpress.com/482/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bandwagonblog.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bandwagonblog.wordpress.com/482/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=482&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bandwagonblog.wordpress.com/2009/02/01/activerecord-one-to-one-relationships/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67d14776ca14b159cf32a7967067b7fe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bandwagonblog</media:title>
		</media:content>
	</item>
		<item>
		<title>ActiveRecord many-to-many Relationships</title>
		<link>http://bandwagonblog.wordpress.com/2009/02/01/activerecord-many-to-many-relationships/</link>
		<comments>http://bandwagonblog.wordpress.com/2009/02/01/activerecord-many-to-many-relationships/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 17:31:12 +0000</pubDate>
		<dc:creator>bandwagonblog</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>

		<guid isPermaLink="false">http://bandwagonblog.wordpress.com/?p=475</guid>
		<description><![CDATA[There are two primary ways to create many-to-many relationships in Rails: has_and_belongs_to_many has_many :through has_and_belongs_to_many A join table is used to link the two activerecord models; unless specified, Rails assumes the tablename is a concatenation of the two joined classes in alphabetical order and joined with an underscore.  If a billing_code has_and_belongs_to_many timesheets, then billing_codes_timesheets [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=475&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There are two primary ways to create many-to-many relationships in Rails:</p>
<ol>
<li>has_and_belongs_to_many</li>
<li>has_many :through</li>
</ol>
<p><span style="text-decoration:underline;"><strong>has_and_belongs_to_many</strong></span><br />
A join table is used to link the two activerecord models; unless specified, Rails assumes the tablename is a concatenation of the two joined classes in alphabetical order and joined with an underscore.  If a billing_code has_and_belongs_to_many timesheets, then billing_codes_timesheets would be the tablename.  The only columns in the table would be billing_code_id and timesheet_id.  There would be no :id column in the table.</p>
<p><em>Self-referential</em> has_and_belongs_to_many (habtm) relationships are also possible.<br />
class BillingCode<br />
has_and_belongs_to_many :related, :join_table =&gt; &#8216;related_billing_codes&#8217;, :foreign_key =&gt; &#8216;first_billing_code_id&#8217;, :association_foreign_key =&gt; &#8216;second_billing_code_id&#8217;, :class_name =&gt; &#8216;BillingCode&#8217;<br />
end</p>
<p><em>Bidirectional Relationships -<strong> </strong></em>habtm relationships do not work bi-directionally by default.<br />
The :insert_sql option must be used to override the normal INSERT statment that Rails would use to associate objects with eachother.</p>
<p>:insert_sql =&gt; &#8216;INSERT INTO related_billing_codes (`first_billing_code_id`, `second_billing_code_id`) VALUES (#{id}, #{record.id}), (#{record.id}, #{id})&#8217;</p>
<p>def delete_records(records)<br />
if sql = @reflection.options[:delete_sql]<br />
records.each { |record| @owner.connection.execute(interpolate_sql(sql,record)) }<br />
else<br />
ids = quoted_record_ids(records)<br />
sql = &#8220;DELETE FROM #{@reflection.options[:join_table]} WHERE #{@reflection.primary_key_name} = #{@owner.quoted_id} AND #{@reflection.association_foreign_key} IN (#{ids})&#8221;<br />
@owner.connection.execute(sql)<br />
end<br />
end</p>
<p>Rails will let you add any columns you want to the join table, but those fields will be marked as read-only; it&#8217;s not possible to save changes to those additional attributes.  habtm relationships should be thought of as the <em>simplest</em> way to establish a many-to-many relationship between models, but problems with habtm begin when you want to add extra data associated with the relationship.</p>
<p><span style="text-decoration:underline;"><strong>has_many</strong> <strong>:through<br />
</strong></span>Rails documentation strongly suggests to upgrade habtm relationships with a has_many :through model, which does a much better job of supporting additional attributes on relationships.</p>
<p><em>John Susser &#8211; </em>&#8220;The has_many :through association allows you to specify a one-to-many relationshipt indirectly via an intermediate join table.  In fact, you can specify more than one relationshipt via the same table, which effectively makes it a replacement for has_and_belongs_to_many.  The biggest advantage is that the join table contains full-fledged model objects complete with primary keys and ancillary data.  No more push_with_attributes; join models just work the same way all your other ActiveRecord models do.</p>
<p><em>Example &#8211; </em></p>
<p>class Client &lt; ActiveRecord::Base<br />
has_many :billable_weeks<br />
has_many :timesheets, :through =&gt; :billable_weeks # relationship<br />
end</p>
<p>class BillableWeek &lt; ActiveRecord::Base # the join model.<br />
belongs_to :client<br />
belongs_to :timesheet<br />
end</p>
<p>class Timesheet &lt; ActiveRecord::Base<br />
has_many :billable_weeks<br />
has_many :clients, :through =&gt; :billable_weeks # inverse relationship<br />
end</p>
<p><em><strong># Therefore, the join model has been upgraded to a full object model.</strong></em></p>
<p>Considerations&#8230;<br />
has_many :through will not let you add an object to the association if both ends of the relationship are unsaved records.  If you use the create method to save the record rather than the &lt;&lt; method, you should be fine.  If you use has_many :through, ActiveRecord will take care of managing the instances of the join model for you.</p>
<p><strong>Aggregating Associations</strong></p>
<p>When using has_many :through to create multiple child associations, you can query the data using find, etc&#8230; but you can&#8217;t append or create new records through them.  If you try creating an instance of an object through its association, the new object&#8217;s database table does not have this object&#8217;s primary key field; it won&#8217;t work. (BillableWeek doesn&#8217;t belong to a user, it belongs to a timesheet, so it doesn&#8217;t have a user_id field).</p>
<p><strong>Join Models &amp; Validations<br />
</strong>You can add validations to the join models, but be careful when creating new instances.</p>
<p>validates_uniqueness_of :client_id, :scope =&gt; :timesheet_id # only 1 of each client per timesheet<br />
validates_presence_of :start_date</p>
<p>timesheet.billable_weeks.create(:start_date =&gt; 1.week.ago) # required field</p>
<p><span style="text-decoration:underline;"><strong>has_many :through Options</strong></span></p>
<p><strong>:source =&gt; </strong>specifies which association to use .. example:<br />
has_many :timesheets, :through =&gt; :billable_weeks, :source =&gt; :sheet # use BillableWeek#sheet</p>
<p><strong>:source_type =&gt; </strong>Used when has_many :through usus a polymorphic belongs_to join model.<br />
The value of the option is the symbol name of the target class.</p>
<p><strong>:uniq</strong> <strong>=&gt; </strong>ensures that only unique objects are in the association collection</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bandwagonblog.wordpress.com/475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bandwagonblog.wordpress.com/475/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bandwagonblog.wordpress.com/475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bandwagonblog.wordpress.com/475/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bandwagonblog.wordpress.com/475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bandwagonblog.wordpress.com/475/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bandwagonblog.wordpress.com/475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bandwagonblog.wordpress.com/475/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bandwagonblog.wordpress.com/475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bandwagonblog.wordpress.com/475/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bandwagonblog.wordpress.com/475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bandwagonblog.wordpress.com/475/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bandwagonblog.wordpress.com/475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bandwagonblog.wordpress.com/475/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=475&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bandwagonblog.wordpress.com/2009/02/01/activerecord-many-to-many-relationships/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67d14776ca14b159cf32a7967067b7fe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bandwagonblog</media:title>
		</media:content>
	</item>
		<item>
		<title>ActiveRecord has_many Association</title>
		<link>http://bandwagonblog.wordpress.com/2009/01/31/activerecord-has_many-association/</link>
		<comments>http://bandwagonblog.wordpress.com/2009/01/31/activerecord-has_many-association/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 02:46:59 +0000</pubDate>
		<dc:creator>bandwagonblog</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[has_many]]></category>

		<guid isPermaLink="false">http://bandwagonblog.wordpress.com/?p=466</guid>
		<description><![CDATA[The has_many association is intuitive; it adds a one-to-many relationship with a class that would declare a belongs_to relationship.  By convention, the singularized form of the relationship name is assumed to be the model class name. class User has_many :timesheets  # by convention, the model would be Timesheet has_many :expense_reports # by convention, the model [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=466&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The <em>has_many</em> association is intuitive; it adds a one-to-many relationship with a class that would declare a <em>belongs_to</em> relationship.  By convention, the singularized form of the relationship name is assumed to be the model class name.</p>
<p>class User<br />
has_many :timesheets  # by convention, the model would be Timesheet<br />
has_many :expense_reports # by convention, the model would be ExpenseReport<br />
end</p>
<p># A lot of customization and power is available to those who know the options you can use in the relationship.</p>
<p><span style="text-decoration:underline;"><strong>has_many Options</strong></span></p>
<p><strong>:after_add </strong># specifies the name of a callback method or Proc to be called after the &lt;&lt; method is used (not create).</p>
<p><strong>:after_remove</strong> # specifies the name of a callback to be called after the delete method is used</p>
<p><strong>:as</strong> # specifies the polymorphic belongs_to association for the related class</p>
<p><strong>:before_add</strong> # specifies the name of callbacks or Procs to be called before the &lt;&lt; method (or concat or push) is called</p>
<p><strong>:before_remove</strong> #specifies the name of callbacks or Procs to be called before the delete method is used</p>
<p><strong>:class_name</strong> # specify a non-conventional classname for the model used in the association</p>
<p><strong>:conditions</strong> # Add extra conditions to the SQL that brings back objects into the association</p>
<p><strong>:counter_sql</strong> # Overrides the query&#8230;. has_many :things, :finder_sql =&gt; &#8216;select * from t where id = #{id}&#8217;</p>
<p><strong>:delete_sql</strong> # Overrides the SQL used to break associations</p>
<p><strong>:dependent =&gt; :delete_all</strong> # all associated objects are deleted w/ 1 SQL command.  No destroy triggers are used.<strong><br />
:dependent =&gt; :destroy_all</strong> # all objects destroyed one at a time with the destroy method.<strong><br />
:dependent =&gt; :nullify</strong> # Default behavior &#8211; nullify (clear) the fk that joins them to the parent.</p>
<p><strong>:extend =&gt; ExtensionModule</strong> # methods that will extend the association collection proxy</p>
<p><strong>:finder_sql =&gt;</strong><strong> </strong>specifies complete SQL to fetch the association.  Count operations are based on this sql as well.</p>
<p><strong>:foreign_key =&gt;</strong><strong> </strong>overrides the convention fk that would normally be used in the SQL for the association</p>
<p><strong>:group =&gt; </strong>attribute for which to use in group by sql</p>
<p><strong>:include =&gt; </strong>array of names used for eager loading (as mentioned in a previous post)</p>
<p><strong>:insert_sql</strong> =&gt; Overrides the SQL to be used to create associtations</p>
<p><strong>:limit =&gt; </strong>appends LIMIT clause to the SQL generated for loading the association</p>
<p><strong>: offset =&gt; </strong>Integer determining the offset from where rows should be fetched</p>
<p><strong>: order =&gt; </strong>used in the ORDER BY clause</p>
<p><strong>:select =&gt; </strong>is SELECT * by default, but can be changed to add additional calculated columns or columns from joins onto the associated object as it is loaded.</p>
<p><strong>:source</strong> and <strong>:source_type</strong> =&gt; used as additional options to help in has_many :through</p>
<p><strong>:table_name</strong> =&gt; overrides the table name that would be used by default in the FROM clause</p>
<p><strong>:through</strong> =&gt; creates a collection association through another association</p>
<p><strong>:uniq =&gt; true </strong># removes duplicate objects from the collection</p>
<p><span style="text-decoration:underline;"><strong>Proxy Methods</strong></span></p>
<p><strong>build(attributes={})</strong> # =&gt; instantiates and links new object in the collection by fk, but doesn&#8217;t save it.</p>
<p><strong>count(*args)</strong> # =&gt; counts the number of associated records in the database with SQL</p>
<p><strong>find(*args) </strong># =&gt; same as the usual ActiveRecord.find method; scope is constrained to associated records and additional conditions specified in the relationship</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bandwagonblog.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bandwagonblog.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bandwagonblog.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bandwagonblog.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bandwagonblog.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bandwagonblog.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bandwagonblog.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bandwagonblog.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bandwagonblog.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bandwagonblog.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bandwagonblog.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bandwagonblog.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bandwagonblog.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bandwagonblog.wordpress.com/466/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=466&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bandwagonblog.wordpress.com/2009/01/31/activerecord-has_many-association/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67d14776ca14b159cf32a7967067b7fe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bandwagonblog</media:title>
		</media:content>
	</item>
		<item>
		<title>ActiveRecord belongs_to Association</title>
		<link>http://bandwagonblog.wordpress.com/2009/01/31/activerecord-belongs_to-association/</link>
		<comments>http://bandwagonblog.wordpress.com/2009/01/31/activerecord-belongs_to-association/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 02:21:43 +0000</pubDate>
		<dc:creator>bandwagonblog</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[belongs_to]]></category>

		<guid isPermaLink="false">http://bandwagonblog.wordpress.com/?p=461</guid>
		<description><![CDATA[Using belongs_to will create a one-to-one relationship from one ActiveRecord object to a single associated object for which it has a foreign key value.  Assigning an object to a belongs_to association will set the fk to the owner object, however the record will not be saved automatically in the database.  Calling belongs_to on a class [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=461&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Using <em><strong>belongs_to</strong></em> will create a one-to-one relationship from one ActiveRecord object to a single associated object for which it has a foreign key value.  Assigning an object to a belongs_to association will set the fk to the owner object, however the record will not be saved automatically in the database.  Calling <em>belongs_to<strong> </strong></em>on a class establishes an attribute of the same name on instances of the class.  The attribute is really a proxy to the related ActiveRecord object.</p>
<p><span style="text-decoration:underline;"><strong>Reloading the Association<br />
</strong></span>Calling the accessor method with a parameter of <em>true</em> will force the association to be reloaded from the database.</p>
<p><span style="text-decoration:underline;"><strong>Building and Creating Related Objects with the Association<br />
</strong></span><em>belongs_to</em> adds factory methods for creating new instances and connecting them with foreign keys automatically.<br />
<em>build_association</em> does not save a new object, but <em>create_association</em> does.</p>
<p><span style="text-decoration:underline;"><strong>belongs_to Options<br />
</strong></span><strong>:class_name</strong> # =&gt; specifies the name of the class for the association. Example:&#8230;</p>
<p>class Timesheet<br />
belongs_to :user<br />
belongs_to :approver, :class_name =&gt; &#8216;User&#8217;  # not an instance of Approver<br />
end</p>
<p><strong>:conditions</strong> # =&gt; Add conditions to a relationship that must be correct in order to be a valid relationship. Example..</p>
<p># Note: conditions only affect how relationships are read from the database, not how they are created.</p>
<p>class Timesheet<br />
belongs_to :approver, :class_name =&gt; &#8216;User&#8217;, :conditions = ['user.authorized_approver = ?', true]<br />
end</p>
<p><strong>:foreign_key</strong> # =&gt; Specifies the fk column used as opposed to the conventional &lt;association&gt;_id column.<br />
belongs_to :administrator :foreign_key =&gt; &#8216;admin_user_id&#8217;<br />
<strong></strong></p>
<p><strong>:counter_cache </strong># =&gt; used to make Rails maintain a counter field automatically called &lt;association<strong>s</strong>&gt;_count<br />
:counter_cache =&gt; true # use the default name for the counter field.<br />
:counter_cache =&gt; &#8216;number_of_children&#8217; # alternate name for the counter field (unconventional)<br />
# be careful to initialize your counter cache column to 0 in the database</p>
<p><strong>:include </strong># =&gt; creates a left outer join to include associations in one query rather than N+1 queries.<br />
# (I have talked about the :include parameter in the post about eager loading.)</p>
<p><strong>:polymorphic =&gt; true<br />
</strong># The type of the related object is stored in the database along with its foreign key.<br />
# When you make a :belongs_to association polymorphic, any other model in the system can fill the role. Example&#8230;.</p>
<p>class Comment &lt; ActiveRecord::Base<br />
belongs_to :subject, :polymorphic =&gt; true<br />
end</p>
<p>class ExpenseReport &lt; ActiveRecord::Base<br />
belongs_to :user<br />
has_many :comments, :as =&gt; :subject<br />
end</p>
<p>class Timesheet &lt; ActiveRecord::Base<br />
belongs_to :user<br />
has_many :comments, :as =&gt; :subject<br />
end</p>
<p># In the database, the <em>comments</em> table would store a <em>subject_type</em> column to store the class name of the associated class.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bandwagonblog.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bandwagonblog.wordpress.com/461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bandwagonblog.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bandwagonblog.wordpress.com/461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bandwagonblog.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bandwagonblog.wordpress.com/461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bandwagonblog.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bandwagonblog.wordpress.com/461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bandwagonblog.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bandwagonblog.wordpress.com/461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bandwagonblog.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bandwagonblog.wordpress.com/461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bandwagonblog.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bandwagonblog.wordpress.com/461/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=461&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bandwagonblog.wordpress.com/2009/01/31/activerecord-belongs_to-association/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67d14776ca14b159cf32a7967067b7fe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bandwagonblog</media:title>
		</media:content>
	</item>
		<item>
		<title>ActiveRecord Associations</title>
		<link>http://bandwagonblog.wordpress.com/2009/01/31/activerecord-associations/</link>
		<comments>http://bandwagonblog.wordpress.com/2009/01/31/activerecord-associations/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 01:29:26 +0000</pubDate>
		<dc:creator>bandwagonblog</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Associations]]></category>
		<category><![CDATA[has_many]]></category>

		<guid isPermaLink="false">http://bandwagonblog.wordpress.com/?p=446</guid>
		<description><![CDATA[Associations in Rails declaratively express relationships between model classes.  Although they appear as macro-like statements in model classes, they are actually methods called on the classes where they are used.  It can be unclear to beginning Rails developers what type of objects are returned by association with these methods.  The objects returned tend to behave [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=446&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Associations in Rails declaratively express relationships between model classes.  Although they appear as macro-like statements in model classes, they are actually methods called on the classes where they are used.  It can be unclear to beginning Rails developers what type of objects are returned by association with these methods.  The objects returned tend to behave like regular Ruby objects and arrays, so it can be easy to think of them actually as arrays or direct model classes; however, it is important to know that these objects are actually associations.</p>
<p>has_many :somethings # would actually be an instance of <em>HasManyAssociation</em></p>
<p>The superclass of all associations is AssociationProxy, which holds the basic structure and functionality of all association proxies.  The proxy object starts out by <em>undef_method</em> most instance methods and relies on sending most method calls to the target of the proxy via <em>method_missing</em>.</p>
<p>object.somethings.respond_to? :proxy_owner # =&gt; true proves that somethings is a proxy.</p>
<p><span style="text-decoration:underline;"><strong>Warning about Associations<br />
</strong></span>Don&#8217;t name associations the same as ActiveRecord::Base instance method names.  When an association is created, a method with that name is added to the model, which would override the inherited method (BAD).</p>
<p><span style="text-decoration:underline;"><strong>One-to-Many Relationships<br />
</strong></span>Consider the following&#8230;</p>
<p>class User &lt; ActiveRecord::Base<br />
has_many :timesheets<br />
has_many :expense_reports<br />
end</p>
<p>class Timesheet &lt; ActiveRecord::Base<br />
belongs_to :user # makes it possible to reference the user to which this belongs<br />
end</p>
<p>class ExpenseReport &lt; ActiveRecord::Base<br />
belongs_to :user  # makes it possible to reference the user to which this belongs<br />
end</p>
<p># When the code is evaluated, Ruby/Rails uses some metaprogramming techniques to add code to your models dynamically add code to your models.  Proxy objects are created to let you manipulate the relationship easily.</p>
<p>dan = User.create :login =&gt; &#8216;demmons&#8217;, :password =&gt; &#8216;xajrwgsdf3&#8242;, :password_confirmation =&gt; &#8216;xajrwgsdf3&#8242;, :email =&gt; &#8216;someone@somewhere.com&#8217;</p>
<p>dan.timesheets # =&gt; ActiveRecord::StatementInvalid: &#8230; no such column: timesheets.user_id&#8230;<br />
Remember &#8211; you have to add a foreign key column to the timesheets and expense_reports tables.<br />
dan = User.find(1) # =&gt; &lt;User:0x35dc85a &#8230; &gt;<br />
dan.timesheets &lt;&lt; Timesheet.new # =&gt; [&lt;Timesheet:0x1234 .. @new_record=true, @attributes={}&gt;]<br />
dan.timesheets # =&gt; [&lt;Timesheet:0x1234 .. @new_record=true, @attributes={}&gt;]</p>
<p><span style="text-decoration:underline;"><strong>Adding Associated Objects to a Collection</strong></span></p>
<p>Adding an object to a <em>has_many </em>collection saves the object automatically unless the parent object is not yet stored in the collection.</p>
<p>dan.timesheets.reload # re-fetches the attributes of an object from the database&#8230;</p>
<p>The <em>&lt;&lt;</em> method set the user_id attribute on the timesheet automatically for you when the item was added.<br />
The &lt;&lt; method takes one or more association objects, and adds each object to the collection.</p>
<p><span style="text-decoration:underline;"><strong>AssociationCollection Methods</strong></span></p>
<p>HasManyAssociation &lt; AssociationCollection<br />
HasAndBelongsToManyAssociation &lt; AssociationCollection</p>
<p><strong>&lt;&lt;(*records)<br />
create(attributes = {})<br />
</strong></p>
<p>Both methods will ad either a single object or many, depending on the input. &lt;&lt; is transactional, and <em>create</em> is not.<br />
The &lt;&lt; method will trigger the <em>:before_add</em> and <em>:after_add</em> callbacks, but the <em>create</em> method does not.  The <em>create</em> method returns the new instance, while the &lt;&lt; method returns the association for easy chaining.  If any records fail to save, the &lt;&lt; method returns false.</p>
<p><strong>clear</strong> # removes all records from the association by clearing the fk.  If the association is configured with the <em>:dependent</em> option set to <em>:delete_all</em>, then clear calls <em>destroy</em> on each one.  The <em>clear</em> method is transactional.</p>
<p><strong>delete(*records) </strong>and <strong>delete_all </strong># remove associations transactionally.<br />
# Note: delete_all will first load all of the objects in the collection from the database before each SQL UPDATE<br />
# The :dependent option on the association defaults to :nullify.  The fk will be updated to null, but won&#8217;t be deleted.<br />
# If the association is configured with :dependent =&gt; :delete_all or :destroy, the records will be deleted.</p>
<p><strong>destroy_all</strong> # begins a transaction &amp; calls destroy on each element, invoking individual DELETE sql</p>
<p><strong>length</strong> # returns size of the collection by loading it and calling size on the array.<br />
# length.zero? is more efficient than calling .emtpy on the array directly.</p>
<p><strong>replace(other_array)</strong> # first deletes objects in the current collection that are not in other_array, and then concat each item in the other_array</p>
<p><strong>size</strong> # returns the size of the array if it has been loaded (otherwise select count(*) query is used).<br />
# use length instead if you will need to access the elements of the collection later anyway.</p>
<p><strong>sum(column, *options) </strong># Computes a summed value on the attribute using SQL.  Requires :group option.</p>
<p><strong>uniq</strong> # Iterates over the collection and creates a Set with the unique values.  <em>id<strong> </strong></em>is used for equality.</p>
<p><strong></strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bandwagonblog.wordpress.com/446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bandwagonblog.wordpress.com/446/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bandwagonblog.wordpress.com/446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bandwagonblog.wordpress.com/446/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bandwagonblog.wordpress.com/446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bandwagonblog.wordpress.com/446/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bandwagonblog.wordpress.com/446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bandwagonblog.wordpress.com/446/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bandwagonblog.wordpress.com/446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bandwagonblog.wordpress.com/446/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bandwagonblog.wordpress.com/446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bandwagonblog.wordpress.com/446/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bandwagonblog.wordpress.com/446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bandwagonblog.wordpress.com/446/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=446&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bandwagonblog.wordpress.com/2009/01/31/activerecord-associations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67d14776ca14b159cf32a7967067b7fe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bandwagonblog</media:title>
		</media:content>
	</item>
		<item>
		<title>ActiveRecord Advanced Finding</title>
		<link>http://bandwagonblog.wordpress.com/2009/01/25/activerecord-advanced-findin/</link>
		<comments>http://bandwagonblog.wordpress.com/2009/01/25/activerecord-advanced-findin/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 22:12:15 +0000</pubDate>
		<dc:creator>bandwagonblog</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[find]]></category>

		<guid isPermaLink="false">http://bandwagonblog.wordpress.com/?p=444</guid>
		<description><![CDATA[Conditions It is common to need to filter a set of results based on some attribute values. You can specify these conditions in a hash as a parameter to the ActiveRecord.find method. They also be specified as an array or directly as a string. User.find(:first, :conditions =&#62; &#8220;login=&#8217;#{login}&#8217; AND password=&#8217;#{password}&#8217;&#8221;) User.find(:first, :conditions =&#62; ["login = [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=444&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Conditions</strong><br />
It is common to need to filter a set of results based on some attribute values.<br />
You can specify these conditions in a hash as a parameter to the ActiveRecord.find method.<br />
They also be specified as an array or directly as a string.</p>
<p>User.find(:first, :conditions =&gt; &#8220;login=&#8217;#{login}&#8217; AND password=&#8217;#{password}&#8217;&#8221;)<br />
User.find(:first, :conditions =&gt; ["login = ? and password = ?", login, password])<br />
User.find(:first, :conditions =&gt; {:login =&gt; login, :password =&gt; password})</p>
<p>The array form should be used when input data needs to be sanitized first.<br />
The string form can be used when you know the parameters are not tainted.<br />
The hash form works similar to the array form, but only equality is a possible condition.</p>
<p>The authenticate_unsafely  method inserts parameters directly into the query; it is vulnerable to SQL-injection attacks.  Malicious users could inject delete statements into the sql, or do other harm.  The authenticate_safely  and authenticate_safely_simply methods will sanitize the parameters before putting them in queries.  Instead of using question marks (?) in the queries, you can use named placeholders and supply a hash of values.</p>
<p>Company.find( :first, [ " name = :name AND division = :div AND created_at &gt; :date ",<br />
{:name =&gt; "37signals", :div =&gt; "First", :date =&gt; '2005-01-01' } ] )</p>
<p><strong>Boolean Conditions</strong><br />
Since databases have different ways of representing boolean values, you should leverage the built in support for converting between Ruby true/false values and the database native type used by the adapter.  Rails will handle the conversion for you transparently as long as you stick with using the helper methods provided to you.  Example:<br />
Timesheet.find(:all, :conditions =&gt; ['submitted=?', true])</p>
<p><strong>Order of Results</strong><br />
Timesheet.find(:all, <img src='http://s1.wp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder =&gt; &#8220;created_at desc&#8221; )  # you can pass anything; the value of <img src='http://s1.wp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder is not validated.</p>
<p><strong>Limit and Offset</strong><br />
The :limit parameter takes an integer, denoting the amount of records to fetch from the database.  The <img src='http://s1.wp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> ffset parameter indicates where the records should start.  If your offset is 40, and your limit is 10, you should obtain the fifth page of 10 results (assuming each page had 10 results previously).</p>
<p><strong>Select Option</strong><br />
The :select option allows you to specify which columns are used in a result set, and also allows you to perform joins to other tables without bringing in columns from those tables back into the results.  If you attempt to obtain the value of an attribute you left out of your select option, a NoMethodError will be raised.  You can select all columns and an additional calculated column by setting your select value with&#8230;. :select =&gt; &#8220;*, &#8221; + &#8220;some_column + some_other_column as some_computed_column&#8221;.</p>
<p><strong>From Option</strong><br />
The :from option allows you to specify the table name of the SQL.  This is where you could also include other tables and views to join with.  Why might you need a parameterized table_name?  Imagine the case where you had module code to be mixed-into a target class.</p>
<p><strong>Group by Option</strong><br />
Use this option if you&#8217;d like the group by clause to be added to your query.  You&#8217;d normally see this option being used in combination with the :select option; grouping by an attribute requires some sort of operation on an attribute to combine row data.  For example, you may need to :select =&gt; &#8216;name, sum(cash) as money&#8217;, :group =&gt; &#8216;name&#8217;.</p>
<p><strong>Explicit Joins</strong><br />
The :join option is useful when doing group by and retrieving data from other tables, but you don&#8217;t want to load the associaed objects from those tables.  Example: Buyer.find(:all, :select =&gt; &#8216;buyers.id, count(carts.id) as cart_count&#8217;, :joins =&gt; &#8216;left join carts on carts.buyer_id=buyers.id&#8217;, :group =&gt; &#8216;buyers.id&#8217;)</p>
<p><strong>Read Only</strong><br />
Specify the :readonly =&gt; true flag if you want to mark the objects as read-only.  You can make local changes to the attribute values, but ActiveRecord will prevent you from saving the changes back to the database.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bandwagonblog.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bandwagonblog.wordpress.com/444/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bandwagonblog.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bandwagonblog.wordpress.com/444/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bandwagonblog.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bandwagonblog.wordpress.com/444/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bandwagonblog.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bandwagonblog.wordpress.com/444/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bandwagonblog.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bandwagonblog.wordpress.com/444/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bandwagonblog.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bandwagonblog.wordpress.com/444/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bandwagonblog.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bandwagonblog.wordpress.com/444/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=444&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bandwagonblog.wordpress.com/2009/01/25/activerecord-advanced-findin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67d14776ca14b159cf32a7967067b7fe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bandwagonblog</media:title>
		</media:content>
	</item>
		<item>
		<title>ActiveRecord Connection Settings</title>
		<link>http://bandwagonblog.wordpress.com/2009/01/25/activerecord-connection-settings/</link>
		<comments>http://bandwagonblog.wordpress.com/2009/01/25/activerecord-connection-settings/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 22:11:06 +0000</pubDate>
		<dc:creator>bandwagonblog</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>

		<guid isPermaLink="false">http://bandwagonblog.wordpress.com/?p=442</guid>
		<description><![CDATA[ActiveRecord::Base.connection.execute( sql ) There might be times when you need to use ActiveRecord&#8217;s underlying database connections outside of the scope of normal use, as in custom scripts or ad-hoc query reporting.  If all of your models that extend ActiveRecord use the same connection, then you can simply access the ActiveRecord::Base.connection attribute.  After obtaining a reference [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=442&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ActiveRecord::Base.connection.execute( sql )<br />
There might be times when you need to use ActiveRecord&#8217;s underlying database connections outside of the scope of normal use, as in custom scripts or ad-hoc query reporting.  If all of your models that extend ActiveRecord use the same connection, then you can simply access the ActiveRecord::Base.connection attribute.  After obtaining a reference to the connection, you can call the execute(string_sql) method on the connection object.</p>
<p>ActiveRecord::ConnectionAdapters::DatabaseStatements<br />
.begin_db_transaction  # begins a txn mannually and turns off auto-commit<br />
.rollback_db_transaction # rolls back the active txn and turns on auto-commit<br />
.commit_db_transaction # commits the transaction and turns on auto-commit<br />
.delete(sql)  # executes a delete; returns number of rows affected<br />
.execute(sql) # executes the sql; returns the result set object for the adapter used.<br />
.insert(sql) # executes the insert; returns the id from the table<br />
.reset_sequence!(table, column, sequence = nil) # sequence = max(column) value<br />
.select_all(sql) # returns an array of record hashes w/ columns as keys.<br />
.select_one(sql) # returns a single hash of keys/values.  use this with limit 1.<br />
.select_value(sql) # returns a single value; the first column of the first row.<br />
.select_values(sql) # returns an array of values in the first column in all rows.<br />
.update(sql) # executes the update; returns the number of rows affected.<br />
.adapter_name # human-readable name of the adapter; e.g. SQLite<br />
.disconnect! # disconnects the active connection<br />
.reconnect!  # closes and opens a new connection in place<br />
.raw_connection # useful for executing proprietary statements or database procs<br />
.supports_count_distcinct? # indicates whether adapter supports count(distinct(&#8230;))<br />
.supports_migrations?  # indicates if the adapter can run migrations<br />
.tables # returns an array of tables in the database.<br />
.verify!(timeout) # verify the connection is valid, only if called &gt; timeout seconds</p>
<p>More configuration options<br />
ActiveRecord::Base.default_timezone = :utc # default is :local<br />
ActiveRecord::Base.allow_concurrency = true #default is false.  just leave it alone. http://permalink.gmane.org/gmane.comp.lang.ruby.mongrel.general/245 for Zed Shaw&#8217;s explanation of the dangers of allow_concurrency.<br />
ActiveRecord::Base.generate_read_methods = true # if false, relies on method_missing (slower)<br />
ActiveRecord::Base.schema_format = :sql # default is ruby.  if it is sql, schema is dumped as db specific sql.<br />
When :ruby is used as the schema_format, an ActiveRecord::Schema file is generated, which could be used to load the schema into any database that supports migrations.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bandwagonblog.wordpress.com/442/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bandwagonblog.wordpress.com/442/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bandwagonblog.wordpress.com/442/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bandwagonblog.wordpress.com/442/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bandwagonblog.wordpress.com/442/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bandwagonblog.wordpress.com/442/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bandwagonblog.wordpress.com/442/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bandwagonblog.wordpress.com/442/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bandwagonblog.wordpress.com/442/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bandwagonblog.wordpress.com/442/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bandwagonblog.wordpress.com/442/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bandwagonblog.wordpress.com/442/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bandwagonblog.wordpress.com/442/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bandwagonblog.wordpress.com/442/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=442&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bandwagonblog.wordpress.com/2009/01/25/activerecord-connection-settings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67d14776ca14b159cf32a7967067b7fe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bandwagonblog</media:title>
		</media:content>
	</item>
		<item>
		<title>ActiveRecord with Multiple Databases</title>
		<link>http://bandwagonblog.wordpress.com/2009/01/25/activerecord-with-multiple-databases/</link>
		<comments>http://bandwagonblog.wordpress.com/2009/01/25/activerecord-with-multiple-databases/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 22:10:24 +0000</pubDate>
		<dc:creator>bandwagonblog</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>

		<guid isPermaLink="false">http://bandwagonblog.wordpress.com/?p=440</guid>
		<description><![CDATA[All classes that inherit from ActiveRecord::Base will use the ActiveRecord::Base.connection established through ActiveRecord::Base.establish_connection, unless otherwise specified in your model. Perform the following steps to create models that use a different database. 1. Edit the config/database.yml file to include details for the additional_database. 2. Create an AdditonalDatabaseRecord &#60; ActiveRecord::Base. establish_connection :additional_database self.abstract_class = true end 3. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=440&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>All classes that inherit from ActiveRecord::Base will use the ActiveRecord::Base.connection established through ActiveRecord::Base.establish_connection, unless otherwise specified in your model.</p>
<p>Perform the following steps to create models that use a different database.</p>
<p>1. Edit the config/database.yml file to include details for the additional_database.<br />
2. Create an AdditonalDatabaseRecord &lt; ActiveRecord::Base.<br />
establish_connection :additional_database<br />
self.abstract_class = true<br />
end<br />
3. Make your model classes that need to use the additonal_database extend the new class you made in step two.  Example: SomeName &lt; AdditionalDatabaseRecord.</p>
<p>Rails will automatically keep database connections in a connection pool inside the ActiveRecord::Base class instance.  Whenever a connection is needed, the ActiveRecord::Base.retrieve_connection method is called, and the appropriate connection is found in the pool.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bandwagonblog.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bandwagonblog.wordpress.com/440/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bandwagonblog.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bandwagonblog.wordpress.com/440/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bandwagonblog.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bandwagonblog.wordpress.com/440/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bandwagonblog.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bandwagonblog.wordpress.com/440/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bandwagonblog.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bandwagonblog.wordpress.com/440/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bandwagonblog.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bandwagonblog.wordpress.com/440/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bandwagonblog.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bandwagonblog.wordpress.com/440/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=440&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bandwagonblog.wordpress.com/2009/01/25/activerecord-with-multiple-databases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67d14776ca14b159cf32a7967067b7fe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bandwagonblog</media:title>
		</media:content>
	</item>
		<item>
		<title>ActiveRecord Locking</title>
		<link>http://bandwagonblog.wordpress.com/2009/01/24/activerecord-locking/</link>
		<comments>http://bandwagonblog.wordpress.com/2009/01/24/activerecord-locking/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 02:32:35 +0000</pubDate>
		<dc:creator>bandwagonblog</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[locking]]></category>

		<guid isPermaLink="false">http://bandwagonblog.wordpress.com/?p=435</guid>
		<description><![CDATA[If you have an application where data may be updated by more than one user at a time, you should consider concurrency issues and race conditions that may arise.  ActiveRecord supports both optimistic and pessimistic locking; there are pros and cons to both locking approaches, and it is to your advantage to know about them. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=435&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you have an application where data may be updated by more than one user at a time, you should consider concurrency issues and race conditions that may arise.  ActiveRecord supports both optimistic and pessimistic locking; there are pros and cons to both locking approaches, and it is to your advantage to know about them.</p>
<p><strong>Optimistic Locking</strong></p>
<p>When collisions are considered infrequent, optimistic locking is considered the way to go.  It takes the approach of leaving the record unlocked, but detects and resolves collisions if they occur.  Implementing this strategy is fairly trivial; add an integer column to the database called <em>lock_version</em>, with a default value of 0.  ActiveRecord&#8217;s behavior will change automatically for you; if two instances of the same record are loaded and saved differently, the first update will succeed, and the second update will raise an ActiveRecord::StaleObjectError.  You can customize the name of the locking column by calling the class method <em>set_locking_column</em> on your model, or globally in your environment.rb file using ActiveRecord::Base.set_locking_column = &#8216;alternate_lock&#8217;.  You will need to catch the raised exception and present the user with an appropriate message, etc.  The main disadvantage to using optimistic locking is that updates are a little slower; validation must be performed by checking the lock column prior to update.  The user also doesn&#8217;t find out about failure until <em>after</em> the attempt to save the data.  That could create an unpleasant user experience.</p>
<p><strong>Pessimistic Locking</strong></p>
<p>Database support is required for pessimistic locking to work.  The database is responsible for locking down specific rows during updates, effectively preventing other users from reading data that is about to be updated, rendering it impossible to read stale data.  Rails takes the approach of working with transactions as in the following example:</p>
<p>TripExpenseReport.transaction do<br />
report = TripExpenseReport.find(1, :lock =&gt; true)<br />
report.approved = true<br />
report.save!<br />
end</p>
<p>Alternatively, you could call an instance method called <em>lock!</em>, which calls <em>reload( :lock =&gt; true )</em> behind the scenes.  Be sure to call <em>lock!</em> before making attribute changes, as they would obviously be lost by the call to reload.</p>
<p>When using pessimistic locking, the select statement that is generated will append <em>FOR UPDATE</em> to the sql, which causes all other connections to be blocked access to the rows returned by the query.  The lock is released once the transaction completes (is committed).  Beware of deadlocks; prevent it by capturing cases where a process fails to complete, etc.  Keep transactions small in scope and time to complete.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bandwagonblog.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bandwagonblog.wordpress.com/435/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bandwagonblog.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bandwagonblog.wordpress.com/435/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bandwagonblog.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bandwagonblog.wordpress.com/435/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bandwagonblog.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bandwagonblog.wordpress.com/435/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bandwagonblog.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bandwagonblog.wordpress.com/435/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bandwagonblog.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bandwagonblog.wordpress.com/435/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bandwagonblog.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bandwagonblog.wordpress.com/435/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bandwagonblog.wordpress.com&amp;blog=1310993&amp;post=435&amp;subd=bandwagonblog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bandwagonblog.wordpress.com/2009/01/24/activerecord-locking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/67d14776ca14b159cf32a7967067b7fe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bandwagonblog</media:title>
		</media:content>
	</item>
	</channel>
</rss>
