Using :through and belongs_to

I'm trying to reduce the mysql calls by using :include or :through and
both methods don't seem to work.

Here's the breakdown: users can have friends and an avatar (image),
so my models are setup like this:

    has_many :images, :as => :imageable
    has_many :friends

    belongs_to :imageable, :polymorphic => true

    # The user
    belongs_to :owner_user, :class_name => "User", :foreign_key =>

    # The friend
    belongs_to :user, :class_name => "User", :foreign_key =>

When I list all of the User's friends with their avatars, a MySql
query is made for each friend image. This adds up to a lot of mysql
requests. So my first plan was to add :include to the Friend

    # The friend
    belongs_to :user, :class_name => "User", :foreign_key =>
"friend_id", :include => :images

For some reason this did not reduce the nubmer of mysql queries, so I
tried a :through association:

    # The friend
    belongs_to :user, :class_name => "User", :foreign_key =>

    has_many :images, :through => :user, :limit => 1

Now I get the following error (most likely do to the foreign_key in
the belongs_to association):

Mysql::Error: Unknown column 'users.friend_id' in 'where clause':
SELECT images.* FROM images INNER JOIN users ON images.imageable_id = AND images.imageable_type = 'User' WHERE ((users.friend_id
= 2)) LIMIT 1

Why is it mapping the foreign_key incorrectly? Is it because I'm
pointing the :through to a belongs_to association? I would appreciate
any solution to this issue.


I’m actually having this exact same problem. Anyone know how to set up this polymorphic through association?