<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: default_value_for Rails plugin: declaratively define default values for ActiveRecord models</title>
	<atom:link href="http://blog.phusion.nl/2008/10/03/47/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.phusion.nl/2008/10/03/47/</link>
	<description></description>
	<lastBuildDate>Thu, 26 Jan 2012 22:32:32 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Tom Maeckelberghe</title>
		<link>http://blog.phusion.nl/2008/10/03/47/comment-page-1/#comment-56884</link>
		<dc:creator>Tom Maeckelberghe</dc:creator>
		<pubDate>Thu, 04 Aug 2011 12:45:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.phusion.nl/?p=47#comment-56884</guid>
		<description>You should keep in mind that you have to be aware of the possible dangers of the plugin. You should know about the inner workings of this plugin before working with it.

How the plugin works

1) remember all the attributes that are supplied to new
ex. Product.new(:name =&gt; &quot;bla&quot;, :price =&gt; &quot;€5&quot;) 
default_value_for will remember that the name and the price are set.

2) the new product attributes will be overwritten with the default values except for price and name

problem case:

class Product
  default_values {:price =&gt; &quot;5&quot;, :currency =&gt; &quot;$&quot;}
  def price= price_and_currency
    self.price = separate_price_form_price_and_currency(price_and_currency)
    self.currency = separate_currency_form_price_and_currency(price_and_currency)
  end
end

Product.new(:name =&gt; &quot;bla&quot;, :price =&gt; &quot;€6&quot;)
attributes are correctly set and not touched by the plugin
1) :name =&gt; &quot;bla&quot;, :price =&gt; &quot;€6&quot;, :currency =&gt; &quot;€&quot;
Then the plugin overwrites attributes with default values except for :price
BUT it will not check on custom setters which alter other attributes
2) :name =&gt; &quot;bla&quot;, :price =&gt; &quot;€6&quot;, currency =&gt; &quot;$&quot;
Our &#039;€&#039; got overwritten by the default value of &#039;$&#039;</description>
		<content:encoded><![CDATA[<p>You should keep in mind that you have to be aware of the possible dangers of the plugin. You should know about the inner workings of this plugin before working with it.</p>
<p>How the plugin works</p>
<p>1) remember all the attributes that are supplied to new<br />
ex. Product.new(:name =&gt; &#8220;bla&#8221;, :price =&gt; &#8220;€5&#8243;)<br />
default_value_for will remember that the name and the price are set.</p>
<p>2) the new product attributes will be overwritten with the default values except for price and name</p>
<p>problem case:</p>
<p>class Product<br />
  default_values {:price =&gt; &#8220;5&#8243;, :currency =&gt; &#8220;$&#8221;}<br />
  def price= price_and_currency<br />
    self.price = separate_price_form_price_and_currency(price_and_currency)<br />
    self.currency = separate_currency_form_price_and_currency(price_and_currency)<br />
  end<br />
end</p>
<p>Product.new(:name =&gt; &#8220;bla&#8221;, :price =&gt; &#8220;€6&#8243;)<br />
attributes are correctly set and not touched by the plugin<br />
1) :name =&gt; &#8220;bla&#8221;, :price =&gt; &#8220;€6&#8243;, :currency =&gt; &#8220;€&#8221;<br />
Then the plugin overwrites attributes with default values except for :price<br />
BUT it will not check on custom setters which alter other attributes<br />
2) :name =&gt; &#8220;bla&#8221;, :price =&gt; &#8220;€6&#8243;, currency =&gt; &#8220;$&#8221;<br />
Our &#8216;€&#8217; got overwritten by the default value of &#8216;$&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blog.phusion.nl/2008/10/03/47/comment-page-1/#comment-9496</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Wed, 26 Aug 2009 22:31:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.phusion.nl/?p=47#comment-9496</guid>
		<description>It doesn&#039;t look like it but I&#039;m not sure if this gets me a workaround for this

class Student &lt; ActiveRecord::Base
 has_many :klasses

 def after_initialize
  klasses.build(:student_id =&gt; id, :status =&gt; &quot;registered&quot;)
 end
end

@student = Student.new
@student.klasses # &lt;-- klass id: nil, student_id: 1, status: registered

thus automatically building the klasses association right when a new student is created.  Unfortunately after_initialize is also called on find which causes problems. Thus the need for a workaround.

Ya I know its a strange example. Sorry again.  No preview.</description>
		<content:encoded><![CDATA[<p>It doesn&#8217;t look like it but I&#8217;m not sure if this gets me a workaround for this</p>
<p>class Student &lt; ActiveRecord::Base<br />
 has_many :klasses</p>
<p> def after_initialize<br />
  klasses.build(:student_id =&gt; id, :status =&gt; &#8220;registered&#8221;)<br />
 end<br />
end</p>
<p>@student = Student.new<br />
@student.klasses # &lt;&#8211; klass id: nil, student_id: 1, status: registered</p>
<p>thus automatically building the klasses association right when a new student is created.  Unfortunately after_initialize is also called on find which causes problems. Thus the need for a workaround.</p>
<p>Ya I know its a strange example. Sorry again.  No preview.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Emma</title>
		<link>http://blog.phusion.nl/2008/10/03/47/comment-page-1/#comment-8610</link>
		<dc:creator>Emma</dc:creator>
		<pubDate>Tue, 28 Jul 2009 20:54:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.phusion.nl/?p=47#comment-8610</guid>
		<description>Sorry the last post came out wrong.

Does it works with nested attributes? 

I&#039;m having problem with this:
&lt;code&gt;
class Person ...)
&lt;/code&gt;
the problem is that p.category now returns the default value.</description>
		<content:encoded><![CDATA[<p>Sorry the last post came out wrong.</p>
<p>Does it works with nested attributes? </p>
<p>I&#8217;m having problem with this:<br />
<code><br />
class Person ...)<br />
</code><br />
the problem is that p.category now returns the default value.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Emma</title>
		<link>http://blog.phusion.nl/2008/10/03/47/comment-page-1/#comment-8609</link>
		<dc:creator>Emma</dc:creator>
		<pubDate>Tue, 28 Jul 2009 20:52:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.phusion.nl/?p=47#comment-8609</guid>
		<description>Does it works with nested attributes? 

I&#039;m having problem with this:

class Person ...)

the problem is that p.category now returns the default value.</description>
		<content:encoded><![CDATA[<p>Does it works with nested attributes? </p>
<p>I&#8217;m having problem with this:</p>
<p>class Person &#8230;)</p>
<p>the problem is that p.category now returns the default value.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: reddyonrails</title>
		<link>http://blog.phusion.nl/2008/10/03/47/comment-page-1/#comment-7611</link>
		<dc:creator>reddyonrails</dc:creator>
		<pubDate>Tue, 07 Jul 2009 17:58:09 +0000</pubDate>
		<guid isPermaLink="false">http://blog.phusion.nl/?p=47#comment-7611</guid>
		<description>Thanks for article. I like uuid example.... 

++ for core addition.</description>
		<content:encoded><![CDATA[<p>Thanks for article. I like uuid example&#8230;. </p>
<p>++ for core addition.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: soren</title>
		<link>http://blog.phusion.nl/2008/10/03/47/comment-page-1/#comment-6457</link>
		<dc:creator>soren</dc:creator>
		<pubDate>Fri, 15 May 2009 18:50:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.phusion.nl/?p=47#comment-6457</guid>
		<description>Re last post

Sorry the beginning did not come out correctly:

It should have shown this:

I use default_value in my model for students to set some inheritance:
class Student &lt; ActiveRecord::Base

  belongs_to :user
  belongs_to :klass

  # Create programatic default values for each element in the database
  default_value_for         :user_id do current_user.id end
  default_value_for         :last_name do current_user.last_name end
  default_value_for         :address do current_user.address end
  default_value_for         :city do current_user.city end
  default_value_for         :zip do current_user.zip end
  default_value_for         :home_phone do current_user.home_phone end

default_value_for has worked fine for me under Rails 2.2.2 in Production and Development, but it fails under Rails 2.3

In Development mode it fails when the student model (per above) is used. Error message is:
(as shown in previous post)

In Production mode it prevents Mongrel from starting up. The Mongrel bootup log is shown in the previous post.

Sorry for the edit mixup</description>
		<content:encoded><![CDATA[<p>Re last post</p>
<p>Sorry the beginning did not come out correctly:</p>
<p>It should have shown this:</p>
<p>I use default_value in my model for students to set some inheritance:<br />
class Student &lt; ActiveRecord::Base</p>
<p>  belongs_to :user<br />
  belongs_to :klass</p>
<p>  # Create programatic default values for each element in the database<br />
  default_value_for         :user_id do current_user.id end<br />
  default_value_for         :last_name do current_user.last_name end<br />
  default_value_for         :address do current_user.address end<br />
  default_value_for         :city do current_user.city end<br />
  default_value_for         :zip do current_user.zip end<br />
  default_value_for         :home_phone do current_user.home_phone end</p>
<p>default_value_for has worked fine for me under Rails 2.2.2 in Production and Development, but it fails under Rails 2.3</p>
<p>In Development mode it fails when the student model (per above) is used. Error message is:<br />
(as shown in previous post)</p>
<p>In Production mode it prevents Mongrel from starting up. The Mongrel bootup log is shown in the previous post.</p>
<p>Sorry for the edit mixup</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: soren</title>
		<link>http://blog.phusion.nl/2008/10/03/47/comment-page-1/#comment-6456</link>
		<dc:creator>soren</dc:creator>
		<pubDate>Fri, 15 May 2009 18:45:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.phusion.nl/?p=47#comment-6456</guid>
		<description>Hi,

I use default_value in my model for students to set some inheritance:
class Student &quot;3c03f9d9352d163861e6ba703f1e633a&quot;, &quot;action&quot;=&gt;&quot;new&quot;, &quot;_method&quot;=&gt;&quot;get&quot;, &quot;adapter&quot;=&gt;&quot;_list_inline_adapter&quot;, &quot;controller&quot;=&gt;&quot;student&quot;}

SystemStackError (stack level too deep):
  app/models/student.rb:7
  vendor/plugins/default_value_for/init.rb:38:in `call&#039;
  vendor/plugins/default_value_for/init.rb:38:in `evaluate&#039;
  vendor/plugins/default_value_for/init.rb:81:in `initialize&#039;
  vendor/plugins/default_value_for/init.rb:79:in `initialize&#039;
  vendor/plugins/default_value_for/init.rb:71:in `initialize&#039;
  app/controllers/student_controller.rb:7

Rendered rescues/_trace (27.2ms)
Rendered rescues/_request_and_response (1.2ms)

Mongrel Bootup in Production mode:
=&gt; Booting Mongrel
=&gt; Rails 2.3.2 application starting on http://0.0.0.0:3000
/rails/formdir-dev/app/models/student.rb:7: warning: Object#id will be deprecated; use Object#object_id
/usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:443:in `load_missing_constant&#039;: uninitialized constant Logging (NameError)
        from /usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:80:in `const_missing&#039;
        from /usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:92:in `const_missing&#039;
        from /usr/lib64/ruby/gems/1.8/gems/logging-1.1.2/lib/logging.rb:479
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:32
/rails/formdir-dev/app/models/student.rb:8: undefined method `last_name&#039; for nil:NilClass (NoMethodError)
        from /rails/formdir-dev/vendor/plugins/default_value_for/init.rb:38:in `call&#039;
        from /rails/formdir-dev/vendor/plugins/default_value_for/init.rb:38:in `evaluate&#039;
        from /rails/formdir-dev/vendor/plugins/default_value_for/init.rb:81:in `initialize&#039;
        from /usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/ordered_hash.rb:69:in `each&#039;
        from /usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/ordered_hash.rb:69:in `each&#039;
        from /rails/formdir-dev/vendor/plugins/default_value_for/init.rb:79:in `initialize&#039;
        from /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2441:in `initialize_without_defaults&#039;
        from /rails/formdir-dev/vendor/plugins/default_value_for/init.rb:71:in `initialize&#039;
         ... 22 levels...
        from /usr/lib64/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:84
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require&#039;
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require&#039;
        from script/server:3</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I use default_value in my model for students to set some inheritance:<br />
class Student &#8220;3c03f9d9352d163861e6ba703f1e633a&#8221;, &#8220;action&#8221;=&gt;&#8221;new&#8221;, &#8220;_method&#8221;=&gt;&#8221;get&#8221;, &#8220;adapter&#8221;=&gt;&#8221;_list_inline_adapter&#8221;, &#8220;controller&#8221;=&gt;&#8221;student&#8221;}</p>
<p>SystemStackError (stack level too deep):<br />
  app/models/student.rb:7<br />
  vendor/plugins/default_value_for/init.rb:38:in `call&#8217;<br />
  vendor/plugins/default_value_for/init.rb:38:in `evaluate&#8217;<br />
  vendor/plugins/default_value_for/init.rb:81:in `initialize&#8217;<br />
  vendor/plugins/default_value_for/init.rb:79:in `initialize&#8217;<br />
  vendor/plugins/default_value_for/init.rb:71:in `initialize&#8217;<br />
  app/controllers/student_controller.rb:7</p>
<p>Rendered rescues/_trace (27.2ms)<br />
Rendered rescues/_request_and_response (1.2ms)</p>
<p>Mongrel Bootup in Production mode:<br />
=&gt; Booting Mongrel<br />
=&gt; Rails 2.3.2 application starting on <a href="http://0.0.0.0:3000" rel="nofollow">http://0.0.0.0:3000</a><br />
/rails/formdir-dev/app/models/student.rb:7: warning: Object#id will be deprecated; use Object#object_id<br />
/usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:443:in `load_missing_constant&#8217;: uninitialized constant Logging (NameError)<br />
        from /usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:80:in `const_missing&#8217;<br />
        from /usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:92:in `const_missing&#8217;<br />
        from /usr/lib64/ruby/gems/1.8/gems/logging-1.1.2/lib/logging.rb:479<br />
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:32<br />
/rails/formdir-dev/app/models/student.rb:8: undefined method `last_name&#8217; for nil:NilClass (NoMethodError)<br />
        from /rails/formdir-dev/vendor/plugins/default_value_for/init.rb:38:in `call&#8217;<br />
        from /rails/formdir-dev/vendor/plugins/default_value_for/init.rb:38:in `evaluate&#8217;<br />
        from /rails/formdir-dev/vendor/plugins/default_value_for/init.rb:81:in `initialize&#8217;<br />
        from /usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/ordered_hash.rb:69:in `each&#8217;<br />
        from /usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/ordered_hash.rb:69:in `each&#8217;<br />
        from /rails/formdir-dev/vendor/plugins/default_value_for/init.rb:79:in `initialize&#8217;<br />
        from /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2441:in `initialize_without_defaults&#8217;<br />
        from /rails/formdir-dev/vendor/plugins/default_value_for/init.rb:71:in `initialize&#8217;<br />
         &#8230; 22 levels&#8230;<br />
        from /usr/lib64/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:84<br />
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require&#8217;<br />
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require&#8217;<br />
        from script/server:3</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Powell</title>
		<link>http://blog.phusion.nl/2008/10/03/47/comment-page-1/#comment-4370</link>
		<dc:creator>Matt Powell</dc:creator>
		<pubDate>Mon, 29 Dec 2008 23:44:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.phusion.nl/?p=47#comment-4370</guid>
		<description>I don&#039;t know whether this is a real bug or just a caveat, but if you call

default_value_for :foo, {}

...then the same hash is shared between instances. Perhaps a call to duping the default would work: in the mean time I am using the block form of default_value_for.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t know whether this is a real bug or just a caveat, but if you call</p>
<p>default_value_for :foo, {}</p>
<p>&#8230;then the same hash is shared between instances. Perhaps a call to duping the default would work: in the mean time I am using the block form of default_value_for.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob Aman</title>
		<link>http://blog.phusion.nl/2008/10/03/47/comment-page-1/#comment-3491</link>
		<dc:creator>Bob Aman</dc:creator>
		<pubDate>Mon, 24 Nov 2008 16:29:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.phusion.nl/?p=47#comment-3491</guid>
		<description>Those aren&#039;t UUIDs, those are just hex digests of big random numbers.  Take a look at RFC 4122.

UUIDs look like this: 343e50b4-ba44-11dd-beb2-001ec2186a45

And they&#039;re not just a random number.  I wrote a Ruby library awhile back for generating real UUIDs, and it&#039;s probably worth using, since it really does give you true uniqueness.  Plus it runs on Ruby 1.8.x, 1.9.x, and JRuby.

http://uuidtools.rubyforge.org/api/

Your code above would become:


&lt;code&gt;
require &quot;uuidtools&quot;
class User  &quot;ee0ec316-ba44-11dd-beb2-001ec2186a45&quot;
User.new.uuid  # =&gt; &quot;ee79dc32-ba44-11dd-beb2-001ec2186a45&quot;
&lt;/code&gt;
</description>
		<content:encoded><![CDATA[<p>Those aren&#8217;t UUIDs, those are just hex digests of big random numbers.  Take a look at RFC 4122.</p>
<p>UUIDs look like this: 343e50b4-ba44-11dd-beb2-001ec2186a45</p>
<p>And they&#8217;re not just a random number.  I wrote a Ruby library awhile back for generating real UUIDs, and it&#8217;s probably worth using, since it really does give you true uniqueness.  Plus it runs on Ruby 1.8.x, 1.9.x, and JRuby.</p>
<p><a href="http://uuidtools.rubyforge.org/api/" rel="nofollow">http://uuidtools.rubyforge.org/api/</a></p>
<p>Your code above would become:</p>
<p><code><br />
require "uuidtools"<br />
class User  "ee0ec316-ba44-11dd-beb2-001ec2186a45"<br />
User.new.uuid  # =&gt; "ee79dc32-ba44-11dd-beb2-001ec2186a45"<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rodrigo</title>
		<link>http://blog.phusion.nl/2008/10/03/47/comment-page-1/#comment-2008</link>
		<dc:creator>Rodrigo</dc:creator>
		<pubDate>Mon, 13 Oct 2008 20:36:43 +0000</pubDate>
		<guid isPermaLink="false">http://blog.phusion.nl/?p=47#comment-2008</guid>
		<description>Oops, submitted too early... :) Anyway, the current workaround is to pass the attribute via the params for the build method (which should not be necessary, since this is a build via the association), like this:

super_market = SuperMarket.create(:name =&gt; ‘Albert Zwijn’, :default_price =&gt; 100)
soap = super_market.products.build(:super_market =&gt; super_market, :name =&gt; ‘Soap’)
soap.price   # =&gt; 100</description>
		<content:encoded><![CDATA[<p>Oops, submitted too early&#8230; <img src='http://blog.phusion.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Anyway, the current workaround is to pass the attribute via the params for the build method (which should not be necessary, since this is a build via the association), like this:</p>
<p>super_market = SuperMarket.create(:name =&gt; ‘Albert Zwijn’, :default_price =&gt; 100)<br />
soap = super_market.products.build(:super_market =&gt; super_market, :name =&gt; ‘Soap’)<br />
soap.price   # =&gt; 100</p>
]]></content:encoded>
	</item>
</channel>
</rss>

