I am upgrading a Rails application from 2.3.10 to 3.0.4 and am running into an issue with updating mo开发者_如何学Godels in my controller. I have been "scoping" model finds in order to prevent users from updating objects that don't belong to them. It works as expected in 2.3, but I get an ActiveRecord::ReadOnlyRecord error with update_attributes in Rails 3.
What is the right way to do this in Rails 3?
Project controller:
def update
  @project = current_user.projects.find(params[:id])
  if @project.update_attributes(params[:project])
    # saved
  else
    # not saved
  end
end
It turns out it was related to using scopes to impersonate active record associations. I was able to fix it by adding .readonly(false) to my scopes.
One possible solution is create new file config/active_record_monkey_patch.rb and add following content in it.
module ReadOnlyFalse
  def self.included(base)
    base.class_eval do
      def readonly?
        false
      end
    end
  end
end
ActiveRecord::Base.send(:include, ReadOnlyFalse)
above code work for all models readonly(false).
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论