Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

has_many creation fails on current master. Works on pre4 #4548

Closed
djpate opened this issue Jul 24, 2016 · 10 comments
Closed

has_many creation fails on current master. Works on pre4 #4548

djpate opened this issue Jul 24, 2016 · 10 comments

Comments

@djpate
Copy link

djpate commented Jul 24, 2016

I tried using the current master to my project.

I have a has_many form such as.

f.inputs do
    f.has_many :product_sub_categories do |psc|
      psc.input :sub_category, :include_blank => false
      psc.input :_destroy, as: :boolean
    end
end

This form works fine on 0.6 and 1.0.0.pre4 but on current master it fails to create because of duplicate key constraint on my model.

I put a breakpoint in data_acces.rb in the save_model method and my object for some reason was created with two identical sub categories even though the params sent to the controler were fine (only one).

Started POST "/admin/products" for 127.0.0.1 at 2016-07-23 19:12:44 -0700
  ActiveRecord::SchemaMigration Load (2.6ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by Admin::ProductsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"7C/cAwEUuR0uC2nKP9DD5M0I/tcdntNF7lxnw08OgmTyRSyZI+Xe95EVdf8pTaKh77X2oTAguA6QWCOpO5HlOw==", "product"=>{"title"=>"ddoo", "description"=>"fooo", "image"=>#<ActionDispatch::Http::UploadedFile:0x00000007cf19b0 @tempfile=#<File:/tmp/RackMultipart20160723-10866-7qvkqr.jpg>, @original_filename="evan dessin-8.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[image]\"; filename=\"evan dessin-8.jpg\"\r\nContent-Type: image/jpeg\r\n">, "sku"=>"80", "price"=>"50", "reduced_price"=>"", "display_order"=>"100", "active"=>"1", "gender"=>"0", "color_ids"=>["", "2", "3"], "size_ids"=>["", "8", "9"], "product_sub_categories_attributes"=>{"0"=>{"sub_category_id"=>"1", "_destroy"=>"0"}}}, "commit"=>"Create Product"}
[3] pry(#<Admin::ProductsController>)> object.product_sub_categories
=> [#<ProductSubCategory:0x00000007d03480
  id: nil,
  product_id: nil,
  sub_category_id: 1>,
 #<ProductSubCategory:0x00000007bb6b18
  id: nil,
  product_id: nil,
  sub_category_id: 1>]
  SQL (0.3ms)  INSERT INTO `products` (`title`, `description`, `sku`, `price`, `gender`, `image_file_name`, `image_content_type`, `image_file_size`, `image_updated_at`, `slug`, `created_at`, `updated_at`) VALUES ('ddoo', 'fooo', '80', 50.0, 0, 'evan_dessin-8.jpg', 'image/jpeg', 1153135, '2016-07-24 02:12:48', 'ddoo-4d84df6c-29af-4aa0-9c1a-76b2922f9073', '2016-07-24 02:14:21', '2016-07-24 02:14:21')
  SQL (0.7ms)  INSERT INTO `product_sub_categories` (`sub_category_id`, `product_id`) VALUES (1, 94)
  SQL (0.4ms)  INSERT INTO `product_sub_categories` (`sub_category_id`, `product_id`) VALUES (1, 94)
   (8.6ms)  ROLLBACK
@timoschilling
Copy link
Member

Please post the full console output of that request, the admin resource file and all related model files.

@dikey94
Copy link

dikey94 commented Jul 24, 2016

Same here.

`Started POST "/admin/outlines" for 192.168.33.1 at 2016-07-24 16:32:01 +0000
Processing by Admin::OutlinesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"MUdakyha3En6qpq4jOFR1p2vmtFU6FLzOdY2kjSE7sodvgqbRbXK+3qqrzgmd0k0ssRIF7Ke1IYYFHlr9L2xWw==", "outline"=>{"name"=>"sfd", "exercise_assignations_attributes"=>{"0"=>{"order"=>"0", "exercise_id"=>"7"}}}, "commit"=>"Utwórz Outline"}
  AdminUser Load (0.1ms)  SELECT  `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 1 ORDER BY `admin_users`.`id` ASC LIMIT 1
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `outlines` (`name`, `created_at`, `updated_at`) VALUES ('sfd', '2016-07-24 16:32:01', '2016-07-24 16:32:01')
  SQL (0.1ms)  INSERT INTO `exercise_assignations` (`exercise_id`, `outline_id`, `created_at`, `updated_at`) VALUES (7, 13, '2016-07-24 16:32:01', '2016-07-24 16:32:01')
  SQL (0.1ms)  INSERT INTO `exercise_assignations` (`exercise_id`, `outline_id`, `created_at`, `updated_at`) VALUES (7, 13, '2016-07-24 16:32:01', '2016-07-24 16:32:01')
   (0.3ms)  COMMIT
Redirected to http://ifizjoterapia:3000/admin/outlines/13
Completed 302 Found in 106ms (ActiveRecord: 1.1ms)`

Admin resource file:

ActiveAdmin.register Outline do
    form do |f|
        f.semantic_errors
        f.inputs "Szczegóły" do
            f.input :name, label: "Nazwa"
            f.has_many :exercise_assignations, heading: "Ćwiczenia", allow_destroy: true, new_record: true do |t|
                t.input :order, label: "Kolejność"
                t.input :exercise, label: "Ćwiczenie"
            end
        end
        f.actions
    end

    permit_params do
        permitted = [:name, :exercise_assignations, :exercises, exercise_assignations_attributes: [:_destroy, :id, :order, :exercise_id]]
    end
end

Exercise model:

class Exercise < ApplicationRecord
    belongs_to :exercise_category
    has_many :exercise_assignations, :dependent => :destroy
    has_many :outlines, through: :exercise_assignations

    validates :picture, attachment_presence: true
    validates :name, length: { minimum: 3 }

    has_attached_file :picture, styles: { full: "1000x427>", medium: "654x279>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
    validates_attachment_content_type :picture, content_type: /\Aimage\/.*\Z/
end

Outline model:

class Outline < ApplicationRecord
    has_many :exercise_assignations, :dependent => :destroy
    has_many :exercises, through: :exercise_assignations
    accepts_nested_attributes_for :exercise_assignations, allow_destroy: true
end

ExerciseAssignation model:

class ExerciseAssignation < ApplicationRecord
    belongs_to :exercise
    belongs_to :outline
end

@gmichokostas
Copy link

I had the same issue, all the queries were running twice. Down grade to 1.0.0.pre4 solved the problem.

@gingray
Copy link

gingray commented Jul 26, 2016

I've face with same issue after some time of research i figure out what was wrong. The main problem in these lines

https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_controller/data_access.rb#L119-L140

      def build_resource
        get_resource_ivar || begin
          resource = build_new_resource
          resource = apply_decorations(resource)
          # problem here
          resource = assign_attributes(resource, resource_params)
          run_build_callbacks resource
          authorize_resource! resource

          set_resource_ivar resource
        end
      end

      def build_new_resource
       # and here
        scoped_collection.send method_for_build, *resource_params
      end

As we can see a double assignment happen. @shaicoleman when you fix a regression seems we get this bug. I think rollback is not good bcz in ur PR you try to fix regression. Thank you.

@railscard
Copy link
Contributor

railscard commented Aug 6, 2016

Same issue. It seems after this PR #4426 we don't need assign_attributes in build_resource anymore

@timoschilling
Copy link
Member

I have reverted #4426, can someone confirm that this bug is fixed on master?

@railscard
Copy link
Contributor

@timoschilling I've checked revert-4426-4424-fix-new-with-cast branch and can confirm that attributes duplication is fixed

@timoschilling
Copy link
Member

@railscard THX

@timoschilling
Copy link
Member

#4426 is now reverted, sorry for the late responce

@shekhar-cardup
Copy link

I am facing the same issue in ActiveAdmin version - 1.3.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants