This project is archived and is in readonly mode.

#6741 new
Ary Borenszweig

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

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>

Attachments

Pages