-
Notifications
You must be signed in to change notification settings - Fork 864
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
Fix count method to work with select statement. #372
Fix count method to work with select statement. #372
Conversation
+1 |
3 similar comments
👍 |
👍 |
+1 |
Works great, thanks! |
👍 |
Awesome! |
👍 |
I'm guessing test coverage is the reason this hasn't been merged. |
Yes, it is possible, I had an issue with installing mysql gem, so I cannot run the tests. But it doesn't matter. It looks, that this error was already fixed in From
|
Can anyone confirm that updating to Rails 4.1.1 fixes this issue? I still get the |
According to blog Rails 4.1.1 is only security fix and includes only code related to security issues. It does not fixes this issue. |
OK. Thanks for the clarification. |
fix_count_with_select_statement (mislav#372) (edariedl)
FYI, bug still exists using 4.1.2.rc1 (rails, active_, action_) |
#303 is a cleaner fix for the count method. There is still an issue with calls to |
Yes, maybe it is a little cleaner, I just took params from rails count() method but it still does not support |
Yah, I don't know either. :-) Also getting other fun test failures when trying to run specs agains Rails 4.1.1. 😦 |
Yes I have some issue with installing mysql gem, maybe its because I don't have installed MySQL database. So I even cannot run specs :). |
But it's still weird Rails 4.1.1 should be only security fix. |
The Gemfile was locked at 4.0.0rc2, so this is 4.1 in general that the specs aren't running on. Lots of stuff with the fixture. Trying to get it working atm. |
Yes, it makes sense, also error fixed in this pull request was created by Rails 4.1.0. |
Maybe this commit rails/rails@afd4d82 in Rails 4.1.2 will fix things. rails/rails#13648. I'll try the rc in a minute. |
nope. |
I thought, according to change log, it will fix it, but after looking into source code. They just fixed |
Maybe |
IMHO you cannot use def size
loaded? ? @records.length : count(:all)
end If you have loaded records from database, which is most of the time, if you have pagination on the bottom of the page. It will count only your loaded records, not all of them. And this is also the reason for redefinition of def count
if limit_value
excluded = [:order, :limit, :offset, :reorder]
excluded << :includes unless eager_loading?
rel = self.except(*excluded)
# TODO: hack. decide whether to keep
rel = rel.apply_finder_options(@wp_count_options) if defined? @wp_count_options
rel.count
else
super
end
end On the third line |
@edariedl Sorry for the late reply. See my comment in #348 (comment) This fix looks simple enough, but I will need a test case for two reasons:
Otherwise I can't risk breaking will_paginate for older Rails versions. |
Afaik, this is resolved with 0c6d08e |
@nathany no, I don’t think so. It’s basically a problem in ActiveRecord, but it isn’t manageable when using will_paginate (since calls to #count are implicit). Example (ActiveRecord 4.1 & will_paginate 3.0.5): User.select(:login, :created_at).count
# Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' created_at) FROM `users`' at line 1: SELECT COUNT(login, created_at) FROM `users`
# In my code I can call
User.select(:login, :created_at).except(:select).count
# or
User.select(:login, :created_at).count(:login)
# When using will_paginate, I can’t
User.select(:login, :created_at).limit(5).page(2).total_pages
# Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds ... (same as above) I checked both 3.0.5 and 0c6d08e. |
@mislav It's ok and I agree that test should be here from the beginning. I add the test and according to CI, everything looks fine for all required versions of rails. |
@nathany Yes, I merged master to the branch. Nice thing to see passing tests and they are broken. I went through all the tests on the Travis CI and everything looks fine, except some deprecations from used libraries. |
def select_for_count(rel) | ||
if rel.select_values.present? | ||
select = rel.select_values.join(", ") | ||
select if select !~ /[,*]/ |
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.
this looks very much like the code removed in rails/rails#10710, which I suppose is as good a solution as any.
Thanks @edariedl. I'm looking forward to the next will_paginate release and Rails 4.1. Whoot! |
Fix count method to work with select statement.
Gonna trust all of your's judgement and go ahead with this. Thanks for the help! |
Tomorrow I can give master another try from our app to see if all is well before uploading a new gem. Thanks @mislav. |
@mislav All is working well here. When are you planning a new gem release? |
@mislav Is this fix included in the "3.0.6" gem release? I'm seeing errors when using |
@nathany Seems like I forgot to backport this to 3-0-stable, I'm sorry. Cutting a v3.0.7 as we speak |
No worries. Thanks @mislav! |
count
method raisesActiveRecord::StatementInvalid
ifselect
method is used in Rails 4.1. This commit fixes will paginate and if work ifselect
method is used.