I am upgrading a Rails application from 2.3.10 to 3.0.4 and am running into an issue with updating models 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With