Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot bulk assign an association as an object #1

Open
mdotterer opened this issue Apr 30, 2009 · 5 comments
Open

Cannot bulk assign an association as an object #1

mdotterer opened this issue Apr 30, 2009 · 5 comments

Comments

@mdotterer
Copy link

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'

@taryneast
Copy link

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 ?

@taryneast
Copy link

For a quick fix try:
CoolResource.new(:owner => current_user.attributes)

???

@taryneast
Copy link

or:
CoolResource.new(:owner_id => current_user.id)

@mdotterer
Copy link
Author

The quick fix I went for was more like:

c = CoolResource.new
c.owner = current_user

@taryneast
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants