开发者

ActiveRecord::ReadOnlyRecord (ActiveRecord::ReadOnlyRecord): during update

开发者 https://www.devze.com 2023-01-27 15:24 出处:网络
I think I\'m still a newbie, but I need your help again. While I\'m working with a habtm association I have problem. I have form where I enter information about a computer. The association habtm is be

I think I'm still a newbie, but I need your help again. While I'm working with a habtm association I have problem. I have form where I enter information about a computer. The association habtm is between machine and operatingsystem.

class Machine < ActiveRecord::Base   
   ....
   has_and_belongs_to_many :operatingsystems, :join_table => "machines_operatingsystems", :readonly => false
   ....
end

class Operatingsystem < ActiveRecord::Base
   ....
   has_and_belongs_to_many :machines, :join_table => "machines_operatingsystems", :readonly => false
   ....
end

I display operating systems with check box.

I modified update of machines controller

 def update
   params[:machine][:operatingsystem_ids] ||= []
   @machine = Machine.find(params[:id], :readonly => false)
   respond_to do |format|
   if @machine.update_attributes(params[:machine])
    flash[:notice] = 'Machine was successfully updated.'
    format.html { redirect_to(@machine) }
    format.xml  { head :ok }
   else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @machine.errors, :status => :unprocessable_entity }
   end
 end
end

But if I edit a machine without for example change name and click on update I have following error:

ActiveRecord::ReadOnlyRecord (ActiveRecord::ReadOnlyRecord):
app/controllers/machines_controller.rb:72
app/controllers/machines_controller.rb:71:in `update'
passenger (2.2.15) lib/phusion_passenger/rack/request_handler.rb:92:in `process_request'
passenger (2.2.15) lib/phusion_passenger/abstract_request_handler.rb:207:in `main_loop'
passenger (2.2.15) lib/phusion_passenger/railz/application_spawner.rb:441:in `start_request_handler'
passenger (2.2.15) lib/phusion_passenger/railz/application_spawner.rb:381:in `handle_spawn_application'
passenger (2.2.15) lib/phusion_passenger/utils.rb:252:in `safe_fork'
passenger (2.2.15) lib/phusion_passenger/railz/application_spawner.rb:377:in `handle_spawn_application'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:352:in `__send__'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:352:in `main_loop'
passenger (2.2.15) lib/开发者_如何学Cphusion_passenger/abstract_server.rb:196:in `start_synchronously'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:163:in `start'
passenger (2.2.15) lib/phusion_passenger/railz/application_spawner.rb:222:in `start'
passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:253:in `spawn_rails_application'
passenger (2.2.15) lib/phusion_passenger/abstract_server_collection.rb:126:in `lookup_or_add'
passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:247:in `spawn_rails_application'
passenger (2.2.15) lib/phusion_passenger/abstract_server_collection.rb:80:in `synchronize'
passenger (2.2.15) lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:246:in `spawn_rails_application'
passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:145:in `spawn_application'
passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:278:in `handle_spawn_application'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:352:in `__send__'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:352:in `main_loop'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:196:in `start_synchronously'

Thanks in advance for your help.


Does your machines_operatingsystems table have any columns on it other than machine_id and operatingsystem_id? If it does, ActiveRecord is marking the record returned by the association as readonly.

One likely culprit would be timestamp columns on the join table. If the migration that creates the machines_operatingsystems includes a call to timestamps, that may be the source of your problem.

0

精彩评论

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