[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:
[code]
create_table :products, :force => true do |t|
t.column :name, :string
t.column :kind, :string, :default=>"pizza"
end
[/code]
I then load a product from a fixture:
[code]
products=YAML::load(foo.yml)
[/code]
#products[0].attributes=>
{: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:
[code]
@product=Product.new(products[0].attributes)
[code]
#@products=>
{: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?
Thanks!
-Cassius
_______________________________________________
talk mailing list
talk@phillyonrails.org
http://lists.phillyonrails.org/mailman/listinfo/talk
_______________________________________________
talk mailing list
talk@phillyonrails.org
http://lists.phillyonrails.org/mailman/listinfo/talk
|
|