On Sun, Jan 11, 2009 at 9:08 PM, Greg Hauptmann
<[email protected]> wrote:
> Why does ActiveRecord allow perception of success when updating an ID,
> however it doesn't really work(i.e. no change in database)?
That is because :id is a special case and is protected from mass assignment.
The reason it "works" or says that it does is because when you submit
an update form, you might have a params hash that looks like:
params[:account] #=> {:id => 2, :name => "Bob"}
and you will then do:
a = Account.find(params[:account][:id])
a.update_attributes(params[:account])
Now... in 99% of cases, you want this update_attributes command to
work, but you definately do NOT want some smartarse out there sending
in a carefully crafted web request that changes the ID value of the
account!
So that's why, update_attributes "silently" ignores any attribute that
is protected from mass assignment.
Actually... it doesn't ignore it. Look in your logs and you will see
something along the lines of "Can't mass assign protected attribute
:id" or something.
For what it's worth, doing:
account.update_attribute(:id, 1)
or
account.id = 1
account.save
Both work.
Hope that helps
Mikel
--
http://lindsaar.net/
Rails, RSpec and Life blog....
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---