composite-primary-keys / composite_primary_keys

Composite Primary Keys support for Active Record
1.03k stars 350 forks source link

"accepts_nested_attributes_for" ok when add but fails when update #141

Closed wanweidong closed 11 years ago

wanweidong commented 11 years ago

I use lasted code with "add test and fix for to_sym exception" class Order < ActiveRecord::Base has_many :order_items, :foreign_key => :order_id accepts_nested_attributes_for :order_items attr_accessible :comment, :order_number, :order_items_attributes end class OrderItem < ActiveRecord::Base self.primary_keys = :order_id, :product_id belongs_to :order, :foreign_key => :order_id attr_accessible :order_id, :product_id, :quantity end

when update

editingorder

Started PUT "/orders/1" for 127.0.0.1 at 2012-12-12 21:25:21 +0800 Processing by OrdersController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"cCDyF/fmu8zM4oGkIwdCH0Ya00rDe HKUy8aPpX1k7XE=", "order"=>{"order_number"=>"123", "comment"=>"321", "order_item s_attributes"=>{"0"=>{"order_id"=>"1", "product_id"=>"1", "quantity"=>"14", "_de stroy"=>"false", "id"=>"[1, 1]"}}}, "commit"=>"Update Order", "id"=>"1"} Order Load (0.0ms) SELECT "orders".* FROM "orders" WHERE "orders "."id" = ? LIMIT 1 [["id", "1"]]  (0.0ms) begin transaction OrderItem Load (1.0ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."order_id" = 1 AND "order_items"."order_id" = 0 AND "order_items"."product_id" IS NULL  (0.0ms) rollback transaction Completed 404 Not Found in 40ms

ActiveRecord::RecordNotFound (Couldn't find OrderItem with ID=[1, 1] for Order with ID=1): app/controllers/orders_controller.rb:62:in block in update' app/controllers/orders_controller.rb:61:inupdate'

source code in "http://github.com/wanweidong/cpk_accepts_nested_attributes_for"

wanweidong commented 11 years ago

I tested, still displays the same error

cfis commented 11 years ago

Ok, next steps on this then? Will there be an updated patch?

lafeber commented 11 years ago

I experience the same issue.

saiga19 commented 11 years ago

https://gist.github.com/aiasfina/5376655

cfis commented 11 years ago

Ok, I think this was fixed with the patch 3 months ago so closing. Please let me know if it is still and issue.

Crystark commented 11 years ago

Hi,

I was using branch ar_4.0.x and just switched to the master as i saw the branch was merged. I'm now getting the following error that seems related to this issue.

rake aborted!
undefined method `unassignable_keys' for #<Mymodel:0x0000000566e248>
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activemodel/lib/active_model/attribute_methods.rb:439:in `method_missing'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/attribute_methods.rb:155:in `method_missing'
/usr/local/rvm/gems/ruby-2.0.0-p195/gems/composite_primary_keys-6.0.0/lib/composite_primary_keys/nested_attributes.rb:51:in `block in assign_nested_attributes_for_collection_association'
/usr/local/rvm/gems/ruby-2.0.0-p195/gems/composite_primary_keys-6.0.0/lib/composite_primary_keys/nested_attributes.rb:46:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p195/gems/composite_primary_keys-6.0.0/lib/composite_primary_keys/nested_attributes.rb:46:in `assign_nested_attributes_for_collection_association'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/nested_attributes.rb:339:in `segoperatornetworks_attributes='
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/attribute_assignment.rb:42:in `public_send'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/attribute_assignment.rb:42:in `_assign_attribute'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/attribute_assignment.rb:29:in `block in assign_attributes'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/attribute_assignment.rb:23:in `each'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/attribute_assignment.rb:23:in `assign_attributes'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/core.rb:182:in `initialize'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/inheritance.rb:27:in `new'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/inheritance.rb:27:in `new'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/validations.rb:39:in `create!'
/home/me/www/myapp/bo/trunk/db/seeds.rb:866:in `<top (required)>'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activesupport/lib/active_support/dependencies.rb:222:in `load'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activesupport/lib/active_support/dependencies.rb:222:in `block in load'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activesupport/lib/active_support/dependencies.rb:213:in `load_dependency'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activesupport/lib/active_support/dependencies.rb:222:in `load'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/railties/lib/rails/engine.rb:540:in `load_seed'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/tasks/database_tasks.rb:153:in `load_seed'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/railties/databases.rake:181:in `block (2 levels) in <top (required)>'
/home/me/.bundler/ruby/2.0.0/rails-3b64d5bac594/activerecord/lib/active_record/railties/databases.rake:140:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `eval'
/usr/local/rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:setup => db:seed

FYI, i'm using rails's branch 4-0-stable and this occurs when i run my seeds.

Mymodel declars accepts_nested_attributes_for :myothermodel, allow_destroy: true

But i do not use the composite keys in either of those models.

ghost commented 11 years ago

I'm also experiencing issues updating nested models in AR 4.0.0 when using the master branch of composite-primary-keys (although ar_4.0.x seems to work fine).

When the params sent to the update action of my VendorDiscussionsController look like this:

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"blahblahblah",
 "vendor_discussion"=>{"name"=>"Jacob is the coolest",
 "description"=>"He's seriously a super swell guy",
 "topics_attributes"=>{"0"=>{"some_field_name"=>"the value",
 "id"=>"5"}}},
 "commit"=>"Update Discussion",
 "id"=>"20"}

and I update the discussion:

if @vendor_discussion.update_attributes(params[:vendor_discussion])

I receive the following error:

wrong number of arguments (4 for 3)
activerecord (4.0.0) lib/active_record/nested_attributes.rb:510:in `assign_to_or_mark_for_destruction'
/Users/jacobcomer/.rvm/gems/ruby-2.0.0-p247/bundler/gems/composite_primary_keys-450b0c543c25/lib/composite_primary_keys/nested_attributes.rb:68:in `block in assign_nested_attributes_for_collection_association'
/Users/jacobcomer/.rvm/gems/ruby-2.0.0-p247/bundler/gems/composite_primary_keys-450b0c543c25/lib/composite_primary_keys/nested_attributes.rb:46:in `each'
/Users/jacobcomer/.rvm/gems/ruby-2.0.0-p247/bundler/gems/composite_primary_keys-450b0c543c25/lib/composite_primary_keys/nested_attributes.rb:46:in `assign_nested_attributes_for_collection_association'
activerecord (4.0.0) lib/active_record/nested_attributes.rb:339:in `topics_attributes='
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:42:in `public_send'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:42:in `_assign_attribute'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:53:in `block in assign_nested_parameter_attributes'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:53:in `each'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:53:in `assign_nested_parameter_attributes'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:33:in `assign_attributes'
activerecord (4.0.0) lib/active_record/persistence.rb:229:in `block in update'
activerecord (4.0.0) lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
activerecord (4.0.0) lib/active_record/transactions.rb:209:in `transaction'
activerecord (4.0.0) lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
activerecord (4.0.0) lib/active_record/persistence.rb:228:in `update'
app/controllers/vendor/vendor_discussions_controller.rb:38:in `update'

but only when using the master branch.

When I switch back to the ar_4.0.x branch (or simply remove nested_attributes.rb from my local copy of the master branch), I don't receive any errors.

chrisbloom7 commented 9 years ago

I'm using CPK v5.0.14, which supposedly includes this patch, in a Rails 3.2.21 app, but I'm still getting this same error.

class AttributeGroup < ActiveRecord::Base
  self.primary_keys = :group_id, :attribute_id
end

[44, 53] in /Users/chrisbloom7/Project/app/controllers/admin/groups_controller.rb
   48:       debugger
=> 49:       if @group.update_attributes(params[:group])
(byebug) params[:group][:attribute_groups_attributes]
{"0"=>{"rank"=>"0", "_destroy"=>"false", "id"=>"[3, 2]"}}
(byebug) @group.attribute_groups
[#<AttributeGroup attribute_id: 2, group_id: 3, rank: 0>]
(byebug) @group.attribute_groups.find("[3,2]")
ActiveRecord::RecordNotFound Exception: Couldn't find AttributeGroup with ID=[3,2] WHERE `attribute_groups`.`group_id` = 3 AND `attribute_groups`.`group_id` = 0 AND `attribute_groups`.`attribute_id` = 2
nil
(byebug) @group.attribute_groups.find("3,2")
#<AttributeGroup attribute_id: 2, group_id: 3, rank: 0>

Did something regress in 5.0.14?

cfis commented 9 years ago

The discussion was about Rails 4.0, looks like you are having issues with Rails 3.2. Each version of Rails is quite different, CPK has to be significantly rewritten with each version so its quite possible these are unrelated issues. I'd say open a new ticket - but in reality you'll need to supply a patch since there isn't anyone to maintain such old versions of CPK (but I can apply a patch if you come up with one).