PS. Just noted the other methods also don't work fyi - see below. That is it doesn't seem to be mass assignment related. ?> ai = AccountItem.find(:first) => #<AccountItem id: 1, account_id: 1, date: "2009-01-11", amount: #<BigDecimal:22e80b8,'0.1E2',4(8)>, balance: #<BigDecimal:22e7f78,'0.0',4(8)>, description: "test", notes: nil, created_at: "2009-01-11 09:47:28", updated_at: "2009-01-11 09:47:28"> >> ai.id = 100000 => 100000 >> ai => #<AccountItem id: 100000, account_id: 1, date: "2009-01-11", amount: #<BigDecimal:22e34b4,'0.1E2',4(8)>, balance: #<BigDecimal:22e3374,'0.0',4(8)>, description: "test", notes: nil, created_at: "2009-01-11 09:47:28", updated_at: "2009-01-11 09:47:28"> >> ai.save => true >> ?> AccountItem.find(:all) => [#<AccountItem id: 1, account_id: 1, date: "2009-01-11", amount: #<BigDecimal:22db098,'0.1E2',4(8)>, balance: #<BigDecimal:22daf58,'0.0',4(8)>, description: "test", notes: nil, created_at: "2009-01-11 09:47:28", updated_at: "2009-01-11 09:47:28">] >>
On Sun, Jan 11, 2009 at 9:38 PM, Mikel Lindsaar <[email protected]> wrote: > > 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.... > > > > -- Greg http://blog.gregnet.org/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

