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

Phenomenon of mysterious value of the position is skipped by one #166

Closed
akicho8 opened this issue Jun 27, 2015 · 2 comments
Closed

Phenomenon of mysterious value of the position is skipped by one #166

akicho8 opened this issue Jun 27, 2015 · 2 comments
Assignees

Comments

@akicho8
Copy link

akicho8 commented Jun 27, 2015

It will become as follows:

book.articles.create!.position # =>  2
book.articles.create!.position # =>  4
book.articles.create!.position # =>  6

Test code

If (A) or (B) or (C) comment out, position value is 1.

require "active_record"
require "acts_as_list"
require "acts_as_list/version"

ActiveRecord::VERSION::STRING     # => "4.2.2"
ActiveRecord::Acts::List::VERSION # => "0.7.2"

ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
ActiveSupport::LogSubscriber.colorize_logging = false
ActiveRecord::Migration.verbose = false
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Schema.define do
  create_table :books do |t|
    t.string :foo
  end
  create_table :articles do |t|
    t.belongs_to :book
    t.integer :position
  end
end

class Book < ActiveRecord::Base
  has_one :article              # (A)
  has_many :articles
  accepts_nested_attributes_for :articles # (B)
end

class Article < ActiveRecord::Base
  belongs_to :book
  acts_as_list scope: :book
  after_create do
    book.update!(:foo => 1)     # (C)
  end
end

book = Book.create!
article = book.articles.create!
article.position                # => 2
# >>    (0.4ms)  CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "foo" varchar) 
# >>    (0.1ms)  CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "book_id" integer, "position" integer) 
# >>    (0.0ms)  begin transaction
# >>   SQL (0.1ms)  INSERT INTO "books" DEFAULT VALUES
# >>    (0.0ms)  commit transaction
# >>    (0.0ms)  begin transaction
# >>   Article Load (0.2ms)  SELECT  "articles".* FROM "articles" WHERE (articles.position IS NOT NULL) AND "articles"."book_id" = ?  ORDER BY articles.position DESC LIMIT 1  [["book_id", 1]]
# >>   SQL (0.1ms)  INSERT INTO "articles" ("book_id", "position") VALUES (?, ?)  [["book_id", 1], ["position", 1]]
# >>   Book Load (0.1ms)  SELECT  "books".* FROM "books" WHERE "books"."id" = ? LIMIT 1  [["id", 1]]
# >>   SQL (0.1ms)  UPDATE "books" SET "foo" = ? WHERE "books"."id" = ?  [["foo", "1"], ["id", 1]]
# >>   Article Load (0.1ms)  SELECT  "articles".* FROM "articles" WHERE (articles.position IS NOT NULL) AND "articles"."book_id" = ?  ORDER BY articles.position DESC LIMIT 1  [["book_id", 1]]
# >>   SQL (0.1ms)  UPDATE "articles" SET "book_id" = ?, "position" = ?, "id" = ? WHERE "articles"."id" = ?  [["book_id", 1], ["position", 2], ["id", 1], ["id", 1]]
# >>    (0.1ms)  SELECT COUNT(*) FROM "articles" WHERE "articles"."book_id" = ? AND (position = 2)  [["book_id", 1]]
# >>    (0.0ms)  commit transaction
@swanandp swanandp self-assigned this Sep 7, 2015
@HannesBenson
Copy link

I've just hit the same issue and found that it was happening when save is called twice while the object is dirty. In our case save was called which fired a callback and the callback saved again. Since position was in the changes hash in both cases the position gets updated twice. Will see when I get some time to write a fix. I resolved it by adding clear_changes_information to the callback but it might be worthwhile removing the position key from the changes hash when the object gets saved with save(validate: false)

@brendon
Copy link
Owner

brendon commented Apr 17, 2016

This should be fixed now. Feel free to let me know if not.

@brendon brendon closed this as completed Apr 17, 2016
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

4 participants