This project is archived and is in readonly mode.
Rails 3: find_or_create_by gives ArgumentError (Unknown keys) with hash parameter in has_many
-
Ben Orenstein
- Importance changed from to Low
-
Ben Orenstein
- State changed from new to open
-
Ben Orenstein
- State changed from open to new
-
Ary Borenszweig
Why low priority? The beauty of Rails 3 is to work with associations like this, and I can see many use cases where you would want to find or create by an attribute and initialize it with other attributes. It's not an edge case, I already wanted to use this in two projects I started recently.
-
Matthew Daubert
Ary,
I suspect the low priority designation is because you an accomplish the same task by changing the last line. I plugged this into your test case and it seems to work fine.
country.people.find_or_create_by_name('bar') { |p| p.age = 30 } -
Flurin
That last one doesn't work either, the block never get's called.
-
Nick Howard
I looked into this. What it looks like is happening is that
ActiveRecord::Association::CollectionProxy's way of handlingfind_or_create_bycalls is written in a manner that does not allow for setting additional attributes when the lookup fails. Any arguments to it are passed to afind_by_call instead of being used to attributes for creating a new object if it didn't exist.I put together a patch for rails master.
https://github.com/rails/rails/pull/358
What do you think?
-
Nick Howard
This has been merged into master https://github.com/rails/rails/commit/f098c80947dee1b420cea4ca647d1...
-
Nick Howard
- Assigned user set to Aaron Patterson
