The ActiveResource::Base#update_attributes method calls a ActiveResource::Base#load method which is defined in activeresource-3.1.3/lib/active_resource/base.rb (line 1255). I am trying to call that load method, rather than simply using update_attributes so that the object is not saved immediately.
-
I tested this with a completely new rails application. I scaffolded a simple object:
rails scaffold obj property1:string -
Then in the rails console:
irb(main):001:0> obj=Obj.new irb(main):002:0> obj.load(:property1=>"data") TypeError: can't convert Hash into String from .../activesupport-3.1.3/lib/active_support/dependencies.rb:234:in `load' from .../activesupport-3.1.3/lib/active_support/dependencies.rb:234:in `load' from .../activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `load_dependency' from .../activesupport-3.1.3/lib/active_support/dependencies.rb:640:in `new_constants_in' from .../activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `load_dependency' from .../activesupport-3.1.3/lib/active_support/dependencies.rb:234:in `load' from (irb):2
I see that activesupport-3.1.3/lib/active_support/dependencies.rb applies its Loadable module to Object, giving every object a load method for loading files, but I can’t figure out why it overrides the ActiveResource::Base#load method and not the other way around.
I am using Rails 3.1.3 and friends.
Update:
I think I’ve answered my own question. I’ve been trying to use ActiveResource methods on ActiveRecord objects. I know that my Rails model classes are descendants of ActiveRecord::Base, but somehow when I was trying to find the code for ActiveRecord::Base#update_attributes I found the code for ActiveResource::Base#update_attributes instead, which looks like this:
def update_attributes(attributes)
load(attributes, false) && save
end
So I’ve been trying to call the load method, which exists for my objects only as provided by activesupport. If I had only looked at ActiveRecord::Base#update_attributes which is
def update_attributes(attributes, options = {})
# The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do
self.assign_attributes(attributes, options)
save
end
end
I would have seen that the assign_attributes method is what I needed.
Instead of doing that, you could use .attributes
The instance has new attributes, but has not been saved.
Read the Docs for more info.