-
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
has_many association with uniq #251
Comments
Did you ever determine a solution to this? I am running into the same issue. |
you can recalculate and set pagination_obj instance variable total_pages, but it will cost you an extra query, as I can remember we solved this by writing the sql instead of generating it :) |
Hello, This is related to a Rails issue that got fixed in master and that will most probably be fixed in Rails 4. There's tons of issues regarding this problem in the Rails repo : To name a few ... Using Rails 3.2.12 I can indeed reproduce the problem :
Using edge rails now :
I don't think they'll be back-porting this to Rails 3, so we'd better close this issue :) |
@anthonyalberto Thanks for researching this. They say it's backported to 3.2-stable. Is that true? |
@mislav oh, didn't notice that! It's actually fixed in 3-2-stable, but it's not included in Rails 3.2.12 Rails 3.2.12 :
Rails 3-2-stable :
|
Closing due to this being an AR bug that is fixed in 4.0 and future versions of 3.2 |
Rails 3 app (PostgreSQL)
The association
has_many :voted_users, :through => :votes, :uniq => true
In console:
I have 2 records associatied:
user.voted_users.count #=> 2
when I use paginate on this:
wp = user.voted_users.paginate :per_page => 1, :page => 1 #so I should get 2 pages
wp.total_pages # => 6
The SQL:
SELECT DISTINCT "users".* FROM "users" INNER JOIN "user_tags" ON "users"."id" = "user_tags"."user_id" INNER JOIN "votes" ON "user_tags"."id" = "votes"."voteable_id" WHERE "votes"."voter_id" = 66 LIMIT 1 OFFSET 0
SELECT DISTINCT COUNT(*) FROM "users" INNER JOIN "user_tags" ON "users"."id" = "user_tags"."user_id" INNER JOIN "votes" ON "user_tags"."id" = "votes"."voteable_id" WHERE "votes"."voter_id" = 66
As you can see the prob is with DISTINCT COUNT
The text was updated successfully, but these errors were encountered: