Composite keys support in nested attributes

278 views
Skip to first unread message

Michel Lacle

unread,
Sep 21, 2011, 3:52:04 PM9/21/11
to Composite Keys for ActiveRecord
Hello Group:

I'm working with a class with a composite key that is an integer and
a string. In an rspec test I'm trying to update this class through a
nested attribute of another class, but I'm getting a duplicate record
instead of the controller updating the existing record.

class Auto < ActiveRecord::Base
set_primary_key "auto_id"

has_many :parameters
accepts_nested_attributes_for :parameters
end

class Parameter < ActiveRecord::Base
set_primary_keys :auto_id, :parameter_code

belongs_to :auto
end

Here is the controller update spec:

auto_params = { auto_id => 1, parameter_code => "RIDE_HEIGHT", value
=> "20mm" }

put :update, :id => 1, :format => "json", :auto => {
:auto_parameter_attributes => [ auto_params ]
}

Now this creates a duplicate record, when 1,RIDE_HEIGHT already
exists.

If I provide auto_params = { id => "1,RIDE_HEIGHT", value => "20mm" }

I get a to_sym method undefined error.

Any ideas as to what I'm messing up?

Thanks,
Michel.

Michel Lacle

unread,
Sep 21, 2011, 4:23:30 PM9/21/11
to Composite Keys for ActiveRecord
Adding back trace:

1) V1::AutosController UPDATE auto_parameters should update the
existing auto parameter
Failure/Error: put :update, :id => @test_auto.id, :format =>
"json", :auto => {
NoMethodError:
undefined method `to_sym' for
[:auto_id, :parameter]:CompositePrimaryKeys::CompositeKeys
# /usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/
active_record/base.rb:1401:in `block in
expand_hash_conditions_for_aggregates'
# /usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/
active_record/base.rb:1396:in `each'
# /usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/
active_record/base.rb:1396:in `expand_hash_conditions_for_aggregates'
# /usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/
active_record/relation/query_methods.rb:232:in `build_where'
# /usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/
active_record/relation/query_methods.rb:95:in `where'
# /usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/
active_record/nested_attributes.rb:396:in
`assign_nested_attributes_for_collection_association'
# /usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/
active_record/nested_attributes.rb:287:in
`banner_parameters_attributes='
# /usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/
active_record/base.rb:1749:in `block in assign_attributes'
# /usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/
active_record/base.rb:1745:in `each'
# /usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/
active_record/base.rb:1745:in `assign_attributes'
# /usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/
active_record/persistence.rb:162:in `block in update_attributes'
# /usr/li

Michel Lacle

unread,
Sep 21, 2011, 4:47:53 PM9/21/11
to Composite Keys for ActiveRecord
I think I found the problem.

In base.rb in expand_hash_conditions_for_aggregates:

The attr variable is usually of type String, but in case of composite
keys it is of type CompositeKeys, which does not have a to_sym method.

Any chance that this can get fixed? I really need it for a product I'm
working on.

Thanks,
Michel.

Hoovy

unread,
Aug 27, 2012, 9:36:43 AM8/27/12
to compos...@googlegroups.com
Same problem here.

Do you have fixed this issue?

Thanks
Hoovy

Jarrod Spillers

unread,
Dec 5, 2012, 6:03:30 PM12/5/12
to compos...@googlegroups.com
I opened an issue for this problem on github along with tests that demonstrate the failure.

https://github.com/drnic/composite_primary_keys/issues/134
Reply all
Reply to author
Forward
0 new messages