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
Reported by Ary Borenszweig | April 25th, 2011 @ 06:08 AM
This is the code that fails, you can put it in a main.rb file and run it:
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
ActiveRecord::Schema.define do
create_table "countries", :force => true do |t|
t.string "name"
end
create_table "people", :force => true do |t|
t.integer "country_id"
t.string "name"
t.integer "age"
end
end
class Country < ActiveRecord::Base
has_many :people
end
class Person < ActiveRecord::Base
belongs_to :country
end
country = Country.create! :name => 'Foo'
country.people.find_or_create_by_name 'bar', :age => 30
I get
/Users/asterite/.rvm/gems/ruby-1.9.2-p136@geochat-rails/gems/activesupport-3.0.3/lib/active_support/core_ext/hash/keys.rb:43:in `assert_valid_keys': Unknown key(s): age (ArgumentError)
from /Users/asterite/.rvm/gems/ruby-1.9.2-p136@geochat-rails/gems/activerecord-3.0.3/lib/active_record/relation/spawn_methods.rb:107:in `apply_finder_options'
from /Users/asterite/.rvm/gems/ruby-1.9.2-p136@geochat-rails/gems/activerecord-3.0.3/lib/active_record/base.rb:934:in `construct_finder_arel'
from /Users/asterite/.rvm/gems/ruby-1.9.2-p136@geochat-rails/gems/activerecord-3.0.3/lib/active_record/base.rb:986:in `method_missing'
from /Users/asterite/.rvm/gems/ruby-1.9.2-p136@geochat-rails/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:443:in `block in method_missing'
from /Users/asterite/.rvm/gems/ruby-1.9.2-p136@geochat-rails/gems/activerecord-3.0.3/lib/active_record/base.rb:1121:in `with_scope'
from /Users/asterite/.rvm/gems/ruby-1.9.2-p136@geochat-rails/gems/activerecord-3.0.3/lib/active_record/associations/association_proxy.rb:203:in `with_scope'
from /Users/asterite/.rvm/gems/ruby-1.9.2-p136@geochat-rails/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:439:in `method_missing'
from /Users/asterite/.rvm/gems/ruby-1.9.2-p136@geochat-rails/gems/activerecord-3.0.3/lib/active_record/associations/association_proxy.rb:151:in `send'
from /Users/asterite/.rvm/gems/ruby-1.9.2-p136@geochat-rails/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:425:in `method_missing'
from main.rb:27:in `<main>'
Comments and changes to this ticket
-
Ben Orenstein April 25th, 2011 @ 03:42 PM
- Importance changed from to Low
-
Ben Orenstein April 25th, 2011 @ 03:43 PM
- State changed from new to open
-
Ben Orenstein April 25th, 2011 @ 03:43 PM
- State changed from open to new
-
Ary Borenszweig April 25th, 2011 @ 03:53 PM
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 April 25th, 2011 @ 05:00 PM
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 }
-
Nick Howard May 1st, 2011 @ 11:34 PM
I looked into this. What it looks like is happening is that
ActiveRecord::Association::CollectionProxy
's way of handlingfind_or_create_by
calls 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 May 6th, 2011 @ 10:31 PM
This has been merged into master https://github.com/rails/rails/commit/f098c80947dee1b420cea4ca647d1...
-
Nick Howard May 6th, 2011 @ 10:31 PM
- Assigned user set to Aaron Patterson
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>