Search code examples
ruby-on-railsruby-on-rails-2

update_attributes() does not works properly


@related=Book.find_all_by_related(@book.related)
if @related.count>1
  @related.each do |b|
    b.update_attributes(params[:book])
  end
end

I'm using rails 2.3.5. for above code, first iteration works fine and show true. but for next iteration, b.update_attributes() shows false . All parameters are reaching in the iterations. Method update_attributes() have any conditions?


Solution

  • If your model specified attr_accessible attributes, only those attributes will be updated.

    Use attr_accessible to prevent mass assignment (by users) of attributes that should not be editable by a user. Mass assignment is used in create and update methods of your standard controller.

    class User < ActiveRecord::Base
      attr_accessible :login, :password
    end
    

    So, doing the following will merrily return true, but will not update the status attribute.

    @user.update_attributes(:status => 'active')
    

    If you want to update the status attribute, you should assign it separately.

    @user.status = 'active'
    save