开发者

Rails 3 scoped finds giving ActiveRecord::ReadOnlyRecord

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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜