Finder_sql and will_paginate cause sql truncation
Reported by James Zwiers | March 11th, 2008 @ 12:47 PM
Hey,
So, I've been working on a rebuild of a complex query (its a friendship style relationship between user accounts). Formerly it was stored using two tables (in addition to the user table), with three records total to represent a single relationship (yes, pretty horrible, and it is having a huge negative impact on our performance due to table bloat).
So, my solution was a single table that would store a single record for each relationship; however, this means that the default behavior in rails (of using a single foreign key in a has_many relationship) wasn't going to work. So, my finder actually looks like:
has_many :friends, :class_name => "User", :finder_sql =>
"SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT users.*, friends.position_one AS position FROM friends
LEFT OUTER JOIN users ON users.id = friends.person_two_id
WHERE friends.person_one_id = #{id}
UNION
SELECT users.*, friends.position_two AS position FROM friends
LEFT OUTER JOIN users ON users.id = friends.person_one_id
WHERE friends.person_two_id = #{id}
ORDER BY friends.position;
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;"
So, now onto the problem (well, it is possible that I'm just missing something and there is a way to do that type of relationship in Rails, but if there is then I'm either blind or its horribly obscure).
Whenever I try to call paginate on that function I get an error (see below), where the error is caused by the finder_sql being truncated to 128 characters.
The error log is located here
Any help is appreciated. I also realize that this may not even be a will_paginate bug, but something related to an issue within Rails or Ruby itself; however, I figured that starting with the plugins first would be the best place :)
Cheers and thanks,
James
Comments and changes to this ticket
-

James Zwiers March 20th, 2008 @ 01:57 PM
Okay, so I was just blind that day. Feel free to ignore this, its not actually truncating. Although, it does look like its not happy with multiple SQL statements inside a finder_sql.
-
Mislav March 30th, 2008 @ 04:48 AM
- → State changed from new to invalid
- → Assigned user changed from Chris Wanstrath to Mislav
Definitely not a will_paginate bug, plus the fact you can't even use paginate on associations with finder_sql.
This hardcore database stuff should be entirely manual. However, after you got the results you could encapsulate them in WillPaginate::Collection that you can render in the view with will_paginate helper.
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
