rails/rails — Issue #4854: Model.find_by_sql using wrong table data to create objects
Author: heeton
Created: 2012-02-02 18:21:34+00:00
State: open

I came across this code before in a bug-fix:

comments = Comment.find_by_sql("SELECT * FROM comments JOIN follows ON comments.user_id = follows.followable_id AND follows.follower_id = #{self.id} LIMIT 10")

It was returning a list of different comments that all had the same id... Wat?
It was the id of the follow in question, wrongly being inserted into the comment object.

This fixed it
SELECT comments.*

Should that 'Model.*' be handled for you when doing Model.find_by_sql?