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

don't work perfectly with default_scope #11

Closed
ayamomiji opened this issue Aug 3, 2011 · 6 comments
Closed

don't work perfectly with default_scope #11

ayamomiji opened this issue Aug 3, 2011 · 6 comments

Comments

@ayamomiji
Copy link

when i set default_scope order: 'position asc' in model, will make some function dont work correct like move_to_bottom.

move_to_bottom uses position desc to get bottom item position, but order position in default_scope will overwrite it.

maybe use max or count function to get bottom item position is better?

@swanandp
Copy link
Contributor

swanandp commented Aug 3, 2011

There has been a pull request, but I just did not get enough time to merge it.

@ghost ghost assigned swanandp Aug 3, 2011
@swanandp
Copy link
Contributor

@ayamomiji - Sorry, I misread this and thought it was about the not null constraint. I am sure the plugin would not work well with default_scope.

The goal behind default_scope is to use it for stuff like :order => 'title ASC'. If you have a where clause in your default scope, then I think at this moment, I would consider it to be beyond the scope of this gem.

But if you have solution in mind, like using unscoped for all lookups, I'll be glad to discuss it.

Cheers,
Swanand

@tanordheim
Copy link
Contributor

Hm, it seems to me like there was some miscommunication here as to what this issue was about?

I'm having the same issue as @ayamomiji mentioned, that I have a sorted model with a default_scope that simply sorts the model by the position column. When having that, adding new records to the end of the list fails as you end up with an SQL-query something like this when the bottom_item method is called in lib/acts_as_list/active_record/acts/list.rb:

SELECT * FROM table WHERE (...) ORDER BY position ASC, position DESC

Where the "position ASC" is defined in the default_scope. and the "position DESC" is specified in the :order clause in the method mentioned above.

At least for me, the solution was simply to append unscoped to the finder. I'm not very familiar with this code base, so I'm not going to claim its the perfect solution - but having default sorting on the position column for a model strikes me as a very common thing to do.

I'd be happy to throw together a pull request with some tests if you want that.

@swanandp swanandp reopened this Aug 19, 2011
@swanandp
Copy link
Contributor

@tanordheim - Your are right, unscoped is the right solution, if you can submit a pull request, I'll be happy to merge it.

@tanordheim
Copy link
Contributor

Done! See #13.

@swanandp
Copy link
Contributor

Merged! :-)

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

3 participants