As no-one else seems to be picking this up, you'll have to manage with my feeble assistance I'm afraid.
I've had a play and I think you are right. find_by_sql should be dynamically creating the attributes based on the field names returned by the SQL statement rather than just the methods defined by the model.
As a guess, I wonder whether its the duplication of id fields that is causing the problem. It might be worth adding a :select argument to the find_by_sql to define and limit the fields being returned by the find_by_sql statement.
So something like:
Code:
@tweets = Tweet.find(:all,
:joins => 'LEFT JOIN users ON users.twitter_id = tweets.twitter_id',
:select => 'users.id,
users.name,
users.twitter_name,
users.twitter_image,
tweets.text,
tweets.date')
Also, if you end up going for a custom SQL solution, you can simplify your code by making either a user or tweets model method to do the work. As all standard models inherit their properties from Base, they already have their own connection method available. So you can do:
Code:
def self.array_of_user_hashes
sql = <<EOF
SELECT *
FROM users
EOF
connection.select_all(sql)
end
This will return an array of hashes. Adding the self to the method name makes this a class method rather than an instance method. Have a look at the RoR api for more information on connection and select_all.
Bookmarks