Chris on 18 Oct 2006 19:21:06 -0000

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PhillyOnRails] ActiveRecord::new() and default table values

Try products[0].clone rather than creating a new object.

Cassius Rosenthal wrote:
This is one of those questions where I knew how to do what I want to, but I can't for the life of me remember how to do it. I should know this, but I just can't seem to find the answer from The Brain (ie., googling didn't help).
Say I have a model Product. I create the table as follows:
create_table :products, :force => true do |t|
t.column :name, :string
t.column :kind, :string, :default=>"pizza"

I then load a product from a fixture:
{:id=>"42", :name=>"apple", :kind=>"fruit"}
So far, so good. Note the id of 42, which is important because this object relates to another model, and I want to preserve the primary keys.

Then I instantiate a new ActiveRecord from this data:
{:id=>"1", :name=>"apple", :kind=>"pizza"}
Ack! ActiveRecord used the table defaults instead of the attributes that I sent to the object. How do I override this behavior?

talk mailing list

_______________________________________________ talk mailing list