I''m actually having this exact same problem. Anyone know how to set up
this
polymorphic through association?
On Nov 28, 2007 9:22 PM, Mozmonkey
<JeremyMailing-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> 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:
>
> User
> has_many :images, :as => :imageable
> has_many :friends
>
> Image
> belongs_to :imageable, :polymorphic => true
>
> Friend
> # The user
> belongs_to :owner_user, :class_name => "User", :foreign_key
=>
> "user_id",
>
> # The friend
> belongs_to :user, :class_name => "User", :foreign_key =>
> "friend_id"
>
>
> 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
> association:
>
> 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:
>
> Friend
> # The friend
> belongs_to :user, :class_name => "User", :foreign_key =>
> "friend_id"
>
> 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 >
users.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.
>
> Thanks,
> Jeremy
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---