Dave M
2005-Oct-13 03:10 UTC
Can''t save a record // update_attributes seems to be ignored
Help!
I have this weird problem whereby I can''t save either
an update, or a new product definition in my rails
app. It used to work, so I think it''s something I''ve
done recently, but for the life of me I can''t figure
out what. Here''s the log for an update_product
invocation:
Processing AdminController#update_product (for
127.0.0.1 at Tue Oct 11 18:24:30 EDT 2005)
Parameters: {"commit"=>"Save",
"product"=>{"image_url"=>"/images/foobar.gif",
"tax"=>"5.0", "price"=>"50.00",
"title"=>"Foobar
product", "date_available(1i)"=>"2005",
"description"=>"90 ways to implement foobar",
"download_url"=>"/downloads/foobar.zip",
"date_available(2i)"=>"1",
"sku"=>"LM0001",
"date_available(3i)"=>"1",
"date_available(4i)"=>"00",
"date_available(5i)"=>"00"},
"action"=>"update_product",
"id"=>"1",
"controller"=>"admin"}
User Load (0.000765) SELECT * FROM users WHERE
users.id = 1 LIMIT 1
User Columns (0.000791) SHOW FIELDS FROM users
Product Load (0.000727) SELECT * FROM products
WHERE products.id = ''1'' LIMIT 1
About to call update_attributes
SQL (0.000247) BEGIN
Product Columns (0.000435) SHOW FIELDS FROM
products
Product Update (0.000667) UPDATE products SET
`download_url` = ''/downloads/foobar.zip'', `title`
''Foobar product'', `tax` = 5.0, `price` = 50.0, `sku`
''LM0001'', `description` = ''90 ways to implement
foobar'', `date_available` = ''2005-01-01 00:00:00'',
`image_url` = ''/images/foobar.gif'' WHERE id = 1
SQL (0.000229) COMMIT
Done with update_attributes
Redirected to
http://127.0.0.1:3000/admin/show_product/1
Completed in 0.02175 (45 reqs/sec) | DB: 0.00386 (17%)
[http://127.0.0.1/admin/update_product/1]
In my admin_controller, I have an action
update_product:
def update_product
@product = Product.find(params[:id])
logger.info("About to call update_attributes")
if @product.update_attributes(params[:product])
logger.info("Done with update_attributes")
flash[''notice''] = ''Product was successfully
updated.''
redirect_to :action => ''show_product'', :id =>
@product
else
logger.info("Done with update_attributes")
render_action ''edit_product''
end
end
But the the call to @product.update_attributes seems
to be ignored. That is, no changes seem to be
applied. Also, with creating a new product, I have
this action:
def create_product
@product = Product.new(params[:product])
if @product.save
flash[''notice''] = ''Product was successfully
created.''
redirect_to :action => ''list_products''
else
render_action ''new_product''
end
end
Same problem here, the values passed into
Product.new() are ignored and the save fails due to
validation errors (i.e. the product object is empty!).
Any ideas? Or some thoughts on how to debug this?
Thanks.
Dave.
__________________________________
Yahoo! Mail - PC Magazine Editors'' Choice 2005
http://mail.yahoo.com
__________________________________
Yahoo! Mail - PC Magazine Editors'' Choice 2005
http://mail.yahoo.com
Joseph Verghese
2005-Oct-13 12:16 UTC
Re: Can''t save a record // update_attributes seems to be ignored
Try @product.update_attributes(params[:product]) @product.update /Joe On 10/13/05, Dave M <davem1972-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> Help! > > I have this weird problem whereby I can''t save either > an update, or a new product definition in my rails > app. It used to work, so I think it''s something I''ve > done recently, but for the life of me I can''t figure > out what. Here''s the log for an update_product > invocation: > > Processing AdminController#update_product (for > 127.0.0.1 at Tue Oct 11 18:24:30 EDT 2005) > Parameters: {"commit"=>"Save", > "product"=>{"image_url"=>"/images/foobar.gif", > "tax"=>"5.0", "price"=>"50.00", "title"=>"Foobar > product", "date_available(1i)"=>"2005", > "description"=>"90 ways to implement foobar", > "download_url"=>"/downloads/foobar.zip", > "date_available(2i)"=>"1", "sku"=>"LM0001", > "date_available(3i)"=>"1", "date_available(4i)"=>"00", > "date_available(5i)"=>"00"}, > "action"=>"update_product", "id"=>"1", > "controller"=>"admin"} > User Load (0.000765) SELECT * FROM users WHERE > users.id = 1 LIMIT 1 > User Columns (0.000791) SHOW FIELDS FROM users > Product Load (0.000727) SELECT * FROM products > WHERE products.id = ''1'' LIMIT 1 > About to call update_attributes > SQL (0.000247) BEGIN > Product Columns (0.000435) SHOW FIELDS FROM > products > Product Update (0.000667) UPDATE products SET > `download_url` = ''/downloads/foobar.zip'', `title` > ''Foobar product'', `tax` = 5.0, `price` = 50.0, `sku` > ''LM0001'', `description` = ''90 ways to implement > foobar'', `date_available` = ''2005-01-01 00:00:00'', > `image_url` = ''/images/foobar.gif'' WHERE id = 1 > SQL (0.000229) COMMIT > Done with update_attributes > Redirected to > http://127.0.0.1:3000/admin/show_product/1 > Completed in 0.02175 (45 reqs/sec) | DB: 0.00386 (17%) > [http://127.0.0.1/admin/update_product/1] > > > In my admin_controller, I have an action > update_product: > > def update_product > @product = Product.find(params[:id]) > logger.info("About to call update_attributes") > if @product.update_attributes(params[:product]) > logger.info("Done with update_attributes") > flash[''notice''] = ''Product was successfully > updated.'' > redirect_to :action => ''show_product'', :id => > @product > else > logger.info("Done with update_attributes") > render_action ''edit_product'' > end > end > > But the the call to @product.update_attributes seems > to be ignored. That is, no changes seem to be > applied. Also, with creating a new product, I have > this action: > > def create_product > @product = Product.new(params[:product]) > if @product.save > flash[''notice''] = ''Product was successfully > created.'' > redirect_to :action => ''list_products'' > else > render_action ''new_product'' > end > end > > Same problem here, the values passed into > Product.new() are ignored and the save fails due to > validation errors (i.e. the product object is empty!). > > Any ideas? Or some thoughts on how to debug this? > > Thanks. > > > Dave. > > > > > > > > __________________________________ > Yahoo! Mail - PC Magazine Editors'' Choice 2005 > http://mail.yahoo.com > > > > > __________________________________ > Yahoo! Mail - PC Magazine Editors'' Choice 2005 > http://mail.yahoo.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- /Joe
Dave M
2005-Oct-13 15:02 UTC
Re: Can''t save a record // update_attributes seems to be ignored
It''s not that it doesn''t try to save the changes (the
update_attributes seems to be issuing an SQL UPDATE,
but with the old values -- adding the @product.update
causes another SQL UPDATE to be issued).
It''s as if the params[] hash is empty (although the
log shows it as not being empty).
For example, if I do the following:
@product.title += "?"
@product.update_attributes(params[:product])
logger.info("product title is #{@product.title}")
And then in the HTML page, I add a ! to the end of the
product title, it''ll actually add the ''?'' to the old
value, but this should''ve been overwritten by the
params[:product] hash contents.
The only think I can think of is that something is
going wrong inside update_attributes and perhaps it''s
not reporting an error when it should be.
Thanks!!!
--- Joseph Verghese <cvjoseph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> Try
>
> @product.update_attributes(params[:product])
> @product.update
>
> /Joe
>
> On 10/13/05, Dave M
<davem1972-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:
> > Help!
> >
> > I have this weird problem whereby I can''t save
> either
> > an update, or a new product definition in my rails
> > app. It used to work, so I think it''s something
> I''ve
> > done recently, but for the life of me I can''t
> figure
> > out what. Here''s the log for an update_product
> > invocation:
> >
> > Processing AdminController#update_product (for
> > 127.0.0.1 at Tue Oct 11 18:24:30 EDT 2005)
> > Parameters: {"commit"=>"Save",
> >
"product"=>{"image_url"=>"/images/foobar.gif",
> > "tax"=>"5.0",
"price"=>"50.00", "title"=>"Foobar
> > product", "date_available(1i)"=>"2005",
> > "description"=>"90 ways to implement foobar",
> > "download_url"=>"/downloads/foobar.zip",
> > "date_available(2i)"=>"1",
"sku"=>"LM0001",
> > "date_available(3i)"=>"1",
> "date_available(4i)"=>"00",
> > "date_available(5i)"=>"00"},
> > "action"=>"update_product",
"id"=>"1",
> > "controller"=>"admin"}
> > User Load (0.000765) SELECT * FROM users WHERE
> > users.id = 1 LIMIT 1
> > User Columns (0.000791) SHOW FIELDS FROM users
> > Product Load (0.000727) SELECT * FROM products
> > WHERE products.id = ''1'' LIMIT 1
> > About to call update_attributes
> > SQL (0.000247) BEGIN
> > Product Columns (0.000435) SHOW FIELDS FROM
> > products
> > Product Update (0.000667) UPDATE products SET
> > `download_url` = ''/downloads/foobar.zip'', `title`
> > > ''Foobar product'', `tax` = 5.0, `price` = 50.0,
> `sku` > > ''LM0001'', `description` = ''90
ways to implement
> > foobar'', `date_available` = ''2005-01-01
00:00:00'',
> > `image_url` = ''/images/foobar.gif'' WHERE id = 1
> > SQL (0.000229) COMMIT
> > Done with update_attributes
> > Redirected to
> > http://127.0.0.1:3000/admin/show_product/1
> > Completed in 0.02175 (45 reqs/sec) | DB: 0.00386
> (17%)
> > [http://127.0.0.1/admin/update_product/1]
> >
> >
> > In my admin_controller, I have an action
> > update_product:
> >
> > def update_product
> > @product = Product.find(params[:id])
> > logger.info("About to call update_attributes")
> > if
> @product.update_attributes(params[:product])
> > logger.info("Done with update_attributes")
> > flash[''notice''] = ''Product was
successfully
> > updated.''
> > redirect_to :action => ''show_product'', :id
> =>
> > @product
> > else
> > logger.info("Done with update_attributes")
> > render_action ''edit_product''
> > end
> > end
> >
> > But the the call to @product.update_attributes
> seems
> > to be ignored. That is, no changes seem to be
> > applied. Also, with creating a new product, I
> have
> > this action:
> >
> > def create_product
> > @product = Product.new(params[:product])
> > if @product.save
> > flash[''notice''] = ''Product was
successfully
> > created.''
> > redirect_to :action => ''list_products''
> > else
> > render_action ''new_product''
> > end
> > end
> >
> > Same problem here, the values passed into
> > Product.new() are ignored and the save fails due
> to
> > validation errors (i.e. the product object is
> empty!).
> >
> > Any ideas? Or some thoughts on how to debug this?
> >
> > Thanks.
> >
> >
> > Dave.
> >
> >
> >
> >
> >
> >
> >
> > __________________________________
> > Yahoo! Mail - PC Magazine Editors'' Choice 2005
> > http://mail.yahoo.com
> >
> >
> >
> >
> > __________________________________
> > Yahoo! Mail - PC Magazine Editors'' Choice 2005
> > http://mail.yahoo.com
> > _______________________________________________
> > Rails mailing list
> > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> >
> http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>
>
> --
> /Joe
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
__________________________________
Start your day with Yahoo! - Make it your home page!
http://www.yahoo.com/r/hs
Chris Hall
2005-Oct-13 15:50 UTC
Re: Can''t save a record // update_attributes seems to be ignored
for debugging purposes, try adding
params[:product].each { |p_key, p_value| logger.info "#{p_key} =
#{p_value}" }
just before the update_attributes and save calls and verify it against
what you typed into your form.
what this sounds like is that whatever you are passing to
update_attributes and save is an empty hash. that''s why
update_attributes is just resaving the existing object with no changes
and save is failing.
On 10/13/05, Dave M <davem1972-/E1597aS9LQAvxtiuMwx3w@public.gmane.org>
wrote:>
> It''s not that it doesn''t try to save the changes (the
> update_attributes seems to be issuing an SQL UPDATE,
> but with the old values -- adding the @product.update
> causes another SQL UPDATE to be issued).
>
> It''s as if the params[] hash is empty (although the
> log shows it as not being empty).
>
> For example, if I do the following:
>
> @product.title += "?"
> @product.update_attributes(params[:product])
> logger.info("product title is #{@product.title}")
>
> And then in the HTML page, I add a ! to the end of the
> product title, it''ll actually add the ''?'' to the
old
> value, but this should''ve been overwritten by the
> params[:product] hash contents.
>
> The only think I can think of is that something is
> going wrong inside update_attributes and perhaps it''s
> not reporting an error when it should be.
>
> Thanks!!!
>
>
> --- Joseph Verghese
<cvjoseph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > Try
> >
> > @product.update_attributes(params[:product])
> > @product.update
> >
> > /Joe
> >
> > On 10/13/05, Dave M
<davem1972-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:
> > > Help!
> > >
> > > I have this weird problem whereby I can''t save
> > either
> > > an update, or a new product definition in my rails
> > > app. It used to work, so I think it''s something
> > I''ve
> > > done recently, but for the life of me I can''t
> > figure
> > > out what. Here''s the log for an update_product
> > > invocation:
> > >
> > > Processing AdminController#update_product (for
> > > 127.0.0.1 at Tue Oct 11 18:24:30 EDT 2005)
> > > Parameters: {"commit"=>"Save",
> > >
"product"=>{"image_url"=>"/images/foobar.gif",
> > > "tax"=>"5.0",
"price"=>"50.00", "title"=>"Foobar
> > > product",
"date_available(1i)"=>"2005",
> > > "description"=>"90 ways to implement
foobar",
> > > "download_url"=>"/downloads/foobar.zip",
> > > "date_available(2i)"=>"1",
"sku"=>"LM0001",
> > > "date_available(3i)"=>"1",
> > "date_available(4i)"=>"00",
> > > "date_available(5i)"=>"00"},
> > > "action"=>"update_product",
"id"=>"1",
> > > "controller"=>"admin"}
> > > User Load (0.000765) SELECT * FROM users WHERE
> > > users.id = 1 LIMIT 1
> > > User Columns (0.000791) SHOW FIELDS FROM users
> > > Product Load (0.000727) SELECT * FROM products
> > > WHERE products.id = ''1'' LIMIT 1
> > > About to call update_attributes
> > > SQL (0.000247) BEGIN
> > > Product Columns (0.000435) SHOW FIELDS FROM
> > > products
> > > Product Update (0.000667) UPDATE products SET
> > > `download_url` = ''/downloads/foobar.zip'',
`title`
> > > > > ''Foobar product'', `tax` = 5.0,
`price` = 50.0,
> > `sku` > > > ''LM0001'', `description` =
''90 ways to implement
> > > foobar'', `date_available` = ''2005-01-01
00:00:00'',
> > > `image_url` = ''/images/foobar.gif'' WHERE id = 1
> > > SQL (0.000229) COMMIT
> > > Done with update_attributes
> > > Redirected to
> > > http://127.0.0.1:3000/admin/show_product/1
> > > Completed in 0.02175 (45 reqs/sec) | DB: 0.00386
> > (17%)
> > > [http://127.0.0.1/admin/update_product/1]
> > >
> > >
> > > In my admin_controller, I have an action
> > > update_product:
> > >
> > > def update_product
> > > @product = Product.find(params[:id])
> > > logger.info("About to call update_attributes")
> > > if
> > @product.update_attributes(params[:product])
> > > logger.info("Done with update_attributes")
> > > flash[''notice''] = ''Product was
successfully
> > > updated.''
> > > redirect_to :action => ''show_product'',
:id
> > =>
> > > @product
> > > else
> > > logger.info("Done with update_attributes")
> > > render_action ''edit_product''
> > > end
> > > end
> > >
> > > But the the call to @product.update_attributes
> > seems
> > > to be ignored. That is, no changes seem to be
> > > applied. Also, with creating a new product, I
> > have
> > > this action:
> > >
> > > def create_product
> > > @product = Product.new(params[:product])
> > > if @product.save
> > > flash[''notice''] = ''Product was
successfully
> > > created.''
> > > redirect_to :action => ''list_products''
> > > else
> > > render_action ''new_product''
> > > end
> > > end
> > >
> > > Same problem here, the values passed into
> > > Product.new() are ignored and the save fails due
> > to
> > > validation errors (i.e. the product object is
> > empty!).
> > >
> > > Any ideas? Or some thoughts on how to debug this?
> > >
> > > Thanks.
> > >
> > >
> > > Dave.
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > __________________________________
> > > Yahoo! Mail - PC Magazine Editors'' Choice 2005
> > > http://mail.yahoo.com
> > >
> > >
> > >
> > >
> > > __________________________________
> > > Yahoo! Mail - PC Magazine Editors'' Choice 2005
> > > http://mail.yahoo.com
> > > _______________________________________________
> > > Rails mailing list
> > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > >
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> > >
> >
> >
> > --
> > /Joe
> > _______________________________________________
> > Rails mailing list
> > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>
>
>
>
> __________________________________
> Start your day with Yahoo! - Make it your home page!
> http://www.yahoo.com/r/hs
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
Dave M
2005-Oct-13 16:20 UTC
Re: Can''t save a record // update_attributes seems to be ignored
The hash is definitely not empty, from the log:
Dumping params[:product] ...
image_url = /images/foobar.gif
tax = 5.0
price = 50.00
title = Foobar product
downloadfile =
date_available(1i) = 2005
imagefile =
description = 90 ways to implement foobar
download_url = /downloads/foobar.zip
date_available(2i) = 1
sku = LM0001
date_available(3i) = 1
date_available(4i) = 00
date_available(5i) = 00
About to call update_attributes
Product Columns (0.000470) SHOW FIELDS FROM
products
SQL (0.002745) BEGIN
Product Load (0.001094) SELECT * FROM products
WHERE sku = ''LM0013'' AND id <> 1 LIMIT 1
Product Update (0.000763) UPDATE products SET
`download_url` = ''/downloads/foobar.zip'', `title`
''Foobar product'', `tax` = 5.0, `price` = 50.00, `sku`
= ''LM0001'', `description` = ''90 ways to implement
foobar'', `date_available` = ''2005-01-01 00:00:00'',
`image_url` = ''/images/foobar.gif'' WHERE id = 1
I can reproduce this also in irb:
irb(main):001:0> p = Product.find(1)
=> #<Product:0x23e51cc @attributes={"tax"=>"5.0",
"image_url"=>"/images/foobar.gif",
"title"=>"Foobar
product", "price"=>"50.00",
"date_available"=>"2005-01-01 00:00:00",
"id"=>"1",
"description"=>"90 ways to implement foobar",
"download_url"=>"/downloads/foobar.zip",
"sku"=>"LM0001"}>
irb(main):002:0> p.update_attributes({:title =>
''Mumblyfoo product''})
=> true
irb(main):003:0> p
=> #<Product:0x23e51cc @attributes={"tax"=>"5.0",
"image_url"=>"/images/foobar.gif",
"title"=>"Foobar
product", "price"=>"50.00",
"date_available"=>"2005-01-01 00:00:00",
"id"=>"1",
"description"=>"90 ways to implement foobar",
"download_url"=>"/downloads/foobar.zip",
"sku"=>"LM0001"}>
It''s got me well and truly stumped!
Thanks.
--- Chris Hall
<christopher.k.hall-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> for debugging purposes, try adding
>
> params[:product].each { |p_key, p_value| logger.info
> "#{p_key} = #{p_value}" }
>
> just before the update_attributes and save calls and
> verify it against
> what you typed into your form.
>
> what this sounds like is that whatever you are
> passing to
> update_attributes and save is an empty hash. that''s
> why
> update_attributes is just resaving the existing
> object with no changes
> and save is failing.
>
> On 10/13/05, Dave M
<davem1972-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:
> >
> > It''s not that it doesn''t try to save the changes
> (the
> > update_attributes seems to be issuing an SQL
> UPDATE,
> > but with the old values -- adding the
> @product.update
> > causes another SQL UPDATE to be issued).
> >
> > It''s as if the params[] hash is empty (although
> the
> > log shows it as not being empty).
> >
> > For example, if I do the following:
> >
> > @product.title += "?"
> > @product.update_attributes(params[:product])
> > logger.info("product title is
> #{@product.title}")
> >
> > And then in the HTML page, I add a ! to the end of
> the
> > product title, it''ll actually add the ''?''
to the
> old
> > value, but this should''ve been overwritten by the
> > params[:product] hash contents.
> >
> > The only think I can think of is that something is
> > going wrong inside update_attributes and perhaps
> it''s
> > not reporting an error when it should be.
> >
> > Thanks!!!
> >
> >
> > --- Joseph Verghese
<cvjoseph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> >
> > > Try
> > >
> > > @product.update_attributes(params[:product])
> > > @product.update
> > >
> > > /Joe
> > >
> > > On 10/13/05, Dave M
<davem1972-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:
> > > > Help!
> > > >
> > > > I have this weird problem whereby I can''t save
> > > either
> > > > an update, or a new product definition in my
> rails
> > > > app. It used to work, so I think it''s
> something
> > > I''ve
> > > > done recently, but for the life of me I can''t
> > > figure
> > > > out what. Here''s the log for an
> update_product
> > > > invocation:
> > > >
> > > > Processing AdminController#update_product (for
> > > > 127.0.0.1 at Tue Oct 11 18:24:30 EDT 2005)
> > > > Parameters: {"commit"=>"Save",
> > > >
"product"=>{"image_url"=>"/images/foobar.gif",
> > > > "tax"=>"5.0",
"price"=>"50.00",
> "title"=>"Foobar
> > > > product",
"date_available(1i)"=>"2005",
> > > > "description"=>"90 ways to implement
foobar",
> > > >
"download_url"=>"/downloads/foobar.zip",
> > > > "date_available(2i)"=>"1",
"sku"=>"LM0001",
> > > > "date_available(3i)"=>"1",
> > > "date_available(4i)"=>"00",
> > > > "date_available(5i)"=>"00"},
> > > > "action"=>"update_product",
"id"=>"1",
> > > > "controller"=>"admin"}
> > > > User Load (0.000765) SELECT * FROM users
> WHERE
> > > > users.id = 1 LIMIT 1
> > > > User Columns (0.000791) SHOW FIELDS FROM
> users
> > > > Product Load (0.000727) SELECT * FROM
> products
> > > > WHERE products.id = ''1'' LIMIT 1
> > > > About to call update_attributes
> > > > SQL (0.000247) BEGIN
> > > > Product Columns (0.000435) SHOW FIELDS
> FROM
> > > > products
> > > > Product Update (0.000667) UPDATE products
> SET
> > > > `download_url` = ''/downloads/foobar.zip'',
> `title`
> > > > > > > ''Foobar product'', `tax` =
5.0, `price` = 50.0,
> > > `sku` > > > > ''LM0001'',
`description` = ''90 ways to
> implement
> > > > foobar'', `date_available` = ''2005-01-01
> 00:00:00'',
> > > > `image_url` = ''/images/foobar.gif'' WHERE
id > 1
> > > > SQL (0.000229) COMMIT
> > > > Done with update_attributes
> > > > Redirected to
> > > > http://127.0.0.1:3000/admin/show_product/1
> > > > Completed in 0.02175 (45 reqs/sec) | DB:
> 0.00386
> > > (17%)
> > > > [http://127.0.0.1/admin/update_product/1]
> > > >
> > > >
> > > > In my admin_controller, I have an action
> > > > update_product:
> > > >
> > > > def update_product
> > > > @product = Product.find(params[:id])
> > > > logger.info("About to call
> update_attributes")
> > > > if
> > > @product.update_attributes(params[:product])
> > > > logger.info("Done with
> update_attributes")
> > > > flash[''notice''] = ''Product
was
> successfully
> > > > updated.''
> > > > redirect_to :action =>
''show_product'',
> :id
> > > =>
> > > > @product
> > > > else
> > > > logger.info("Done with
> update_attributes")
> > > > render_action ''edit_product''
> > > > end
> > > > end
> > > >
> > > > But the the call to @product.update_attributes
> > > seems
> > > > to be ignored. That is, no changes seem to be
> > > > applied. Also, with creating a new product, I
> > > have
> > > > this action:
> > > >
> > > > def create_product
> > > > @product = Product.new(params[:product])
> > > > if @product.save
> > > > flash[''notice''] = ''Product
was
> successfully
> > > > created.''
> > > > redirect_to :action =>
''list_products''
> > > > else
> > > > render_action ''new_product''
> > > > end
> > > > end
> > > >
> > > > Same problem here, the values passed into
> > > > Product.new() are ignored and the save fails
> due
> > > to
> > > > validation errors (i.e. the product object is
> > > empty!).
> > > >
> > > > Any ideas? Or some thoughts on how to debug
> this?
> > > >
> > > > Thanks.
> > > >
> > > >
> > > > Dave.
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > __________________________________
> > > > Yahoo! Mail - PC Magazine Editors'' Choice 2005
> > > > http://mail.yahoo.com
> > > >
> > > >
> > > >
> > > >
>
=== message truncated ==
__________________________________
Yahoo! Mail - PC Magazine Editors'' Choice 2005
http://mail.yahoo.com
Dave M
2005-Oct-13 23:54 UTC
Re: Can''t save a record // update_attributes seems to be ignored
>From debugging update_attributes (see my other post ondebugging with script/console), I''ve found that the incoming attributes list is obliterated by remove_attributes_protected_from_mass_assignment inside the attributes= method. It looks like since I had declared a couple of synthetic attributes via attr_accessible:, that self.class.accessible_attributes is now only returning the attributes explicitly marked as accessible. However, if I remove attr_accessible, I don''t have this (calling self.class.accessible_attributes returns nil). It seems that if you declare any "new" attributes as accessible, you also need to declare everything from the schema. Seems like a bug to me, but now I know how to work around it. Thanks. --- Dave M <davem1972-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> > The hash is definitely not empty, from the log: > > Dumping params[:product] ... > image_url = /images/foobar.gif > tax = 5.0 > price = 50.00 > title = Foobar product > downloadfile = > date_available(1i) = 2005 > imagefile = > description = 90 ways to implement foobar > download_url = /downloads/foobar.zip > date_available(2i) = 1 > sku = LM0001 > date_available(3i) = 1 > date_available(4i) = 00 > date_available(5i) = 00 > About to call update_attributes > Product Columns (0.000470) SHOW FIELDS FROM > products > SQL (0.002745) BEGIN > Product Load (0.001094) SELECT * FROM products > WHERE sku = ''LM0013'' AND id <> 1 LIMIT 1 > Product Update (0.000763) UPDATE products SET > `download_url` = ''/downloads/foobar.zip'', `title` > ''Foobar product'', `tax` = 5.0, `price` = 50.00, > `sku` > = ''LM0001'', `description` = ''90 ways to implement > foobar'', `date_available` = ''2005-01-01 00:00:00'', > `image_url` = ''/images/foobar.gif'' WHERE id = 1 > > > I can reproduce this also in irb: > > irb(main):001:0> p = Product.find(1) > => #<Product:0x23e51cc @attributes={"tax"=>"5.0", > "image_url"=>"/images/foobar.gif", "title"=>"Foobar > product", "price"=>"50.00", > "date_available"=>"2005-01-01 00:00:00", "id"=>"1", > "description"=>"90 ways to implement foobar", > "download_url"=>"/downloads/foobar.zip", > "sku"=>"LM0001"}> > irb(main):002:0> p.update_attributes({:title => > ''Mumblyfoo product''}) > => true > irb(main):003:0> p > => #<Product:0x23e51cc @attributes={"tax"=>"5.0", > "image_url"=>"/images/foobar.gif", "title"=>"Foobar > product", "price"=>"50.00", > "date_available"=>"2005-01-01 00:00:00", "id"=>"1", > "description"=>"90 ways to implement foobar", > "download_url"=>"/downloads/foobar.zip", > "sku"=>"LM0001"}> > > > It''s got me well and truly stumped! > > Thanks. > > > > --- Chris Hall <christopher.k.hall-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > for debugging purposes, try adding > > > > params[:product].each { |p_key, p_value| > logger.info > > "#{p_key} = #{p_value}" } > > > > just before the update_attributes and save calls > and > > verify it against > > what you typed into your form. > > > > what this sounds like is that whatever you are > > passing to > > update_attributes and save is an empty hash. > that''s > > why > > update_attributes is just resaving the existing > > object with no changes > > and save is failing. > > > > On 10/13/05, Dave M <davem1972-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > > > > > It''s not that it doesn''t try to save the changes > > (the > > > update_attributes seems to be issuing an SQL > > UPDATE, > > > but with the old values -- adding the > > @product.update > > > causes another SQL UPDATE to be issued). > > > > > > It''s as if the params[] hash is empty (although > > the > > > log shows it as not being empty). > > > > > > For example, if I do the following: > > > > > > @product.title += "?" > > > @product.update_attributes(params[:product]) > > > logger.info("product title is > > #{@product.title}") > > > > > > And then in the HTML page, I add a ! to the end > of > > the > > > product title, it''ll actually add the ''?'' to the > > old > > > value, but this should''ve been overwritten by > the > > > params[:product] hash contents. > > > > > > The only think I can think of is that something > is > > > going wrong inside update_attributes and perhaps > > it''s > > > not reporting an error when it should be. > > > > > > Thanks!!! > > > > > > > > > --- Joseph Verghese <cvjoseph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Try > > > > > > > > @product.update_attributes(params[:product]) > > > > @product.update > > > > > > > > /Joe > > > > > > > > On 10/13/05, Dave M <davem1972-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> > wrote: > > > > > Help! > > > > > > > > > > I have this weird problem whereby I can''t > save > > > > either > > > > > an update, or a new product definition in my > > rails > > > > > app. It used to work, so I think it''s > > something > > > > I''ve > > > > > done recently, but for the life of me I > can''t > > > > figure > > > > > out what. Here''s the log for an > > update_product > > > > > invocation: > > > > > > > > > > Processing AdminController#update_product > (for > > > > > 127.0.0.1 at Tue Oct 11 18:24:30 EDT 2005) > > > > > Parameters: {"commit"=>"Save", > > > > > > "product"=>{"image_url"=>"/images/foobar.gif", > > > > > "tax"=>"5.0", "price"=>"50.00", > > "title"=>"Foobar > > > > > product", "date_available(1i)"=>"2005", > > > > > "description"=>"90 ways to implement > foobar", > > > > > "download_url"=>"/downloads/foobar.zip", > > > > > "date_available(2i)"=>"1", "sku"=>"LM0001", > > > > > "date_available(3i)"=>"1", > > > > "date_available(4i)"=>"00", > > > > > "date_available(5i)"=>"00"}, > > > > > "action"=>"update_product", "id"=>"1", > > > > > "controller"=>"admin"} > > > > > User Load (0.000765) SELECT * FROM users > > WHERE > > > > > users.id = 1 LIMIT 1 > > > > > User Columns (0.000791) SHOW FIELDS FROM > > users > > > > > Product Load (0.000727) SELECT * FROM > > products > > > > > WHERE products.id = ''1'' LIMIT 1 > > > > > About to call update_attributes > > > > > SQL (0.000247) BEGIN > > > > > Product Columns (0.000435) SHOW FIELDS > > FROM > > > > > products > > > > > Product Update (0.000667) UPDATE > products > > SET > > > > > `download_url` = ''/downloads/foobar.zip'', > > `title` > > > > > > > > > ''Foobar product'', `tax` = 5.0, `price` > 50.0, > > > > `sku` > > > > > ''LM0001'', `description` = ''90 ways to > > implement > > > > > foobar'', `date_available` = ''2005-01-01 > > 00:00:00'', > > > > > `image_url` = ''/images/foobar.gif'' WHERE id > > > 1 > > > > > SQL (0.000229) COMMIT > > > > > Done with update_attributes > > > > > Redirected to >=== message truncated == __________________________________ Yahoo! Mail - PC Magazine Editors'' Choice 2005 http://mail.yahoo.com
Frodo Larik
2006-Feb-10 15:45 UTC
[Rails] Can''t save a record // update_attributes seems to be ignored
I just struggled along this problem... According to "Agile web Development with Rails" : The method attr_accessible() allows you to list the attributes that may be assigned automatically--all other attributes will be protected. This is particularly usefull if the structure of the underlying table is liable to change, as any new columns you add wil be protected by default. So use attr_accessor instead of attr_accessible I also couldn''t find the attr_accesible in the official ruby implementation On 10/14/05, Dave M <davem1972@yahoo.com> wrote:> > > >From debugging update_attributes (see my other post on > debugging with script/console), I''ve found that the > incoming attributes list is obliterated by > remove_attributes_protected_from_mass_assignment > inside the attributes= method. > > It looks like since I had declared a couple of > synthetic attributes via attr_accessible:, that > self.class.accessible_attributes is now only returning > the attributes explicitly marked as accessible. > However, if I remove attr_accessible, I don''t have > this (calling self.class.accessible_attributes returns > nil). It seems that if you declare any "new" > attributes as accessible, you also need to declare > everything from the schema. > > Seems like a bug to me, but now I know how to work > around it. > > Thanks. > > > --- Dave M <davem1972@yahoo.com> wrote: > > > > > The hash is definitely not empty, from the log: > > > > Dumping params[:product] ... > > image_url = /images/foobar.gif > > tax = 5.0 > > price = 50.00 > > title = Foobar product > > downloadfile > > date_available(1i) = 2005 > > imagefile > > description = 90 ways to implement foobar > > download_url = /downloads/foobar.zip > > date_available(2i) = 1 > > sku = LM0001 > > date_available(3i) = 1 > > date_available(4i) = 00 > > date_available(5i) = 00 > > About to call update_attributes > > Product Columns (0.000470) SHOW FIELDS FROM > > products > > SQL (0.002745) BEGIN > > Product Load (0.001094) SELECT * FROM products > > WHERE sku = ''LM0013'' AND id <> 1 LIMIT 1 > > Product Update (0.000763) UPDATE products SET > > `download_url` = ''/downloads/foobar.zip'', `title` > > ''Foobar product'', `tax` = 5.0, `price` = 50.00, > > `sku` > > = ''LM0001'', `description` = ''90 ways to implement > > foobar'', `date_available` = ''2005-01-01 00:00:00'', > > `image_url` = ''/images/foobar.gif'' WHERE id = 1 > > > > > > I can reproduce this also in irb: > > > > irb(main):001:0> p = Product.find(1) > > => #<Product:0x23e51cc @attributes={"tax"=>"5.0", > > "image_url"=>"/images/foobar.gif", "title"=>"Foobar > > product", "price"=>"50.00", > > "date_available"=>"2005-01-01 00:00:00", "id"=>"1", > > "description"=>"90 ways to implement foobar", > > "download_url"=>"/downloads/foobar.zip", > > "sku"=>"LM0001"}> > > irb(main):002:0> p.update_attributes({:title => > > ''Mumblyfoo product''}) > > => true > > irb(main):003:0> p > > => #<Product:0x23e51cc @attributes={"tax"=>"5.0", > > "image_url"=>"/images/foobar.gif", "title"=>"Foobar > > product", "price"=>"50.00", > > "date_available"=>"2005-01-01 00:00:00", "id"=>"1", > > "description"=>"90 ways to implement foobar", > > "download_url"=>"/downloads/foobar.zip", > > "sku"=>"LM0001"}> > > > > > > It''s got me well and truly stumped! > > > > Thanks. > > > > > > > > --- Chris Hall <christopher.k.hall@gmail.com> wrote: > > > > > for debugging purposes, try adding > > > > > > params[:product].each { |p_key, p_value| > > logger.info > > > "#{p_key} = #{p_value}" } > > > > > > just before the update_attributes and save calls > > and > > > verify it against > > > what you typed into your form. > > > > > > what this sounds like is that whatever you are > > > passing to > > > update_attributes and save is an empty hash. > > that''s > > > why > > > update_attributes is just resaving the existing > > > object with no changes > > > and save is failing. > > > > > > On 10/13/05, Dave M <davem1972@yahoo.com> wrote: > > > > > > > > It''s not that it doesn''t try to save the changes > > > (the > > > > update_attributes seems to be issuing an SQL > > > UPDATE, > > > > but with the old values -- adding the > > > @product.update > > > > causes another SQL UPDATE to be issued). > > > > > > > > It''s as if the params[] hash is empty (although > > > the > > > > log shows it as not being empty). > > > > > > > > For example, if I do the following: > > > > > > > > @product.title += "?" > > > > @product.update_attributes(params[:product]) > > > > logger.info("product title is > > > #{@product.title}") > > > > > > > > And then in the HTML page, I add a ! to the end > > of > > > the > > > > product title, it''ll actually add the ''?'' to the > > > old > > > > value, but this should''ve been overwritten by > > the > > > > params[:product] hash contents. > > > > > > > > The only think I can think of is that something > > is > > > > going wrong inside update_attributes and perhaps > > > it''s > > > > not reporting an error when it should be. > > > > > > > > Thanks!!! > > > > > > > > > > > > --- Joseph Verghese <cvjoseph@gmail.com> wrote: > > > > > > > > > Try > > > > > > > > > > @product.update_attributes(params[:product]) > > > > > @product.update > > > > > > > > > > /Joe > > > > > > > > > > On 10/13/05, Dave M <davem1972@yahoo.com> > > wrote: > > > > > > Help! > > > > > > > > > > > > I have this weird problem whereby I can''t > > save > > > > > either > > > > > > an update, or a new product definition in my > > > rails > > > > > > app. It used to work, so I think it''s > > > something > > > > > I''ve > > > > > > done recently, but for the life of me I > > can''t > > > > > figure > > > > > > out what. Here''s the log for an > > > update_product > > > > > > invocation: > > > > > > > > > > > > Processing AdminController#update_product > > (for > > > > > > 127.0.0.1 at Tue Oct 11 18:24:30 EDT 2005) > > > > > > Parameters: {"commit"=>"Save", > > > > > > > > "product"=>{"image_url"=>"/images/foobar.gif", > > > > > > "tax"=>"5.0", "price"=>"50.00", > > > "title"=>"Foobar > > > > > > product", "date_available(1i)"=>"2005", > > > > > > "description"=>"90 ways to implement > > foobar", > > > > > > "download_url"=>"/downloads/foobar.zip", > > > > > > "date_available(2i)"=>"1", "sku"=>"LM0001", > > > > > > "date_available(3i)"=>"1", > > > > > "date_available(4i)"=>"00", > > > > > > "date_available(5i)"=>"00"}, > > > > > > "action"=>"update_product", "id"=>"1", > > > > > > "controller"=>"admin"} > > > > > > User Load (0.000765) SELECT * FROM users > > > WHERE > > > > > > users.id = 1 LIMIT 1 > > > > > > User Columns (0.000791) SHOW FIELDS FROM > > > users > > > > > > Product Load (0.000727) SELECT * FROM > > > products > > > > > > WHERE products.id = ''1'' LIMIT 1 > > > > > > About to call update_attributes > > > > > > SQL (0.000247) BEGIN > > > > > > Product Columns (0.000435) SHOW FIELDS > > > FROM > > > > > > products > > > > > > Product Update (0.000667) UPDATE > > products > > > SET > > > > > > `download_url` = ''/downloads/foobar.zip'', > > > `title` > > > > > > > > > > > ''Foobar product'', `tax` = 5.0, `price` > > 50.0, > > > > > `sku` > > > > > > ''LM0001'', `description` = ''90 ways to > > > implement > > > > > > foobar'', `date_available` = ''2005-01-01 > > > 00:00:00'', > > > > > > `image_url` = ''/images/foobar.gif'' WHERE id > > > > > 1 > > > > > > SQL (0.000229) COMMIT > > > > > > Done with update_attributes > > > > > > Redirected to > > > === message truncated ==> > > > > > __________________________________ > Yahoo! Mail - PC Magazine Editors'' Choice 2005 > http://mail.yahoo.com > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Sincerely, Frodo Larik -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060210/1cfe0551/attachment-0001.html