Cannot bulk assign an association as an object
Bulk assigning an object as an association fails.
For example: CoolResource.new(:owner => current_user) raises:
ArgumentError: expected an attributes Hash, got #User:...
activeresource/lib/active_resource/base.rb:750:in 'load' activeresource/lib/active_resource/base.rb:521:in 'initialize' hyperactive_resource/lib/hyperactive_resource.rb:338:in 'new' hyperactive_resource/lib/hyperactive_resource.rb:338:in 'load_array' hyperactive_resource/lib/hyperactive_resource.rb:338:in 'map' hyperactive_resource/lib/hyperactive_resource.rb:338:in 'load_array' hyperactive_resource/lib/hyperactive_resource.rb:308:in 'load' hyperactive_resource/lib/hyperactive_resource.rb:303:in 'each' hyperactive_resource/lib/hyperactive_resource.rb:303:in 'load' hyperactive_resource/lib/hyperactive_resource.rb:14:in 'initialize'
For a quick fix try: CoolResource.new(:owner => current_user.attributes)
???
or: CoolResource.new(:owner_id => current_user.id)
Hmm, weird. Looking at your backtrace, it seems to think that current_user is an Array. Can you try again and also do a current_user.inspect to show us what is being passed in ?
The quick fix I went for was more like:
c = CoolResource.new c.owner = current_user
Cool. Good to know there's at least a workaround. I have this vague feeling that ActiveRecord has similar issues with accepting real objects for associations on creation. Not sure why - it seems pretty sensible that it should accept it... Still, I find it weird that HyRes seems to think that current_user is an Array.
could you try: u = User.find(1) c = CoolResource.new(:owner => u)
just to be sure it's not something weird coming out of current_user ??? It's probably not - but it'd be good to be certain.
I've had a go at it myself and I can say that using "find" it seems to work for me... but then I had to hack together a fake HyRes object. In my case my CoolResource has_one :user is yours has_one or belongs_to?