-
-
Notifications
You must be signed in to change notification settings - Fork 356
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
Don't try to reload new records #182
Conversation
This should fix brendon#122.
@@ -458,7 +458,7 @@ def check_scope | |||
end | |||
|
|||
def reload_position | |||
self.reload | |||
self.reload unless new_record? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant self
detected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the help, Hound.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is self
required here? I get the funny feeling that it's for STI support...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not required. I left it there because I wanted to change as little as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to remove it but I remember doing something like this before and then it got put back because it broke the way STI worked. But I think it was more to do with returning the class name for the AAL model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea I can't see the harm in removing it if all the tests pass. It's not like it's retuning anything we need.
I removed the redundant |
Thanks for that. Can you give me a good use case for trying to delete a records that isn't persisted yet? I can see your example in the issue, but that one seems a bit contrived. Also, I'm wondering if we should do:
|
I hit this case in a test suite. I was using non-persisted objects and destroying them to test callbacks. Something like this: it do
list = List.new
element = list.elements.build
expect(element).to receive(:destroy)
list.destroy
end |
Ah I see. I suppose it's reasonable that we don't raise an error when Rails doesn't raise an error upon destroying non-persisted objects. How about
Saves a few lines. Though, the only reason we're reloading is for the purposes of the |
¯_(ツ)_/¯ Doesn't matter to me. I'm not familiar with the internals of this library. However you want to get it done is good with me. |
Lol, such enthusiasm ;) I'm thinking perhaps that row should be locked before deletion. Could you try updating your branch with this instead? before_destroy :lock! If that causes the same error for you then try: before_destroy :lock!, if: :persisted? if that works then I think that's the best outcome :) |
Neither of those worked. Here's the test case I dropped into class List < ActiveRecord::Base
has_many :elements
end
class Element < ActiveRecord::Base
belongs_to :list
acts_as_list scope: :list
end
class X < ActsAsListTestCase
def setup
ActiveRecord::Schema.define do
create_table :lists
create_table :elements do |table|
table.integer :list_id
table.integer :position
end
end
end
def test_x
list = List.new
element = list.elements.build
begin
list.elements.each &:destroy
assert true
rescue
assert false
end
end
end |
That's strange. Try this branch: https://github.com/swanandp/acts_as_list/tree/lock-before-destroy
def lock!(lock = true)
reload(:lock => lock) if persisted?
self
end I've even added a failing test. It's as simple as building a new (un-persisted) record (I don't think it has to be from a |
👍 That looks good! |
Very good. I'll close this PR and merge my branch: #201 Glad it works for you :) |
This should fix #122.