I got the Manager model and SoccerTeam . The manager owns many football teams; Also, the manager can comment on football teams and can comment on other managers:
manager.rb
soccer_team.rb
comment.rb
belongs_to :commentable, :polymorphic => true belongs_to :author, :class_name => Manager
Now, if I have a manager commenting on SoccerTeam, I expect to find:
- A
Comment object in manager.reviews and in soccer_team.comments SoccerTeam object in manager.observed_teamsManager object in soccer_team.observers
While everything works well for the first and third points, when I call manager.observed_teams , I always get an empty array. To get a list of football teams, the manager commented on what I need to use:
manager.reviews.collect{ |review| Kernel.const_get(review.commentable_type).find(review.commentable_id) if review.commentable_type == "SoccerTeam" }
What is ugly. I expect a simple manager.observed_teams to work ... why is it not?
Edit
I went further, understanding why this does not work. In fact, genrated SQL:
SELECT "soccer_teams".* FROM "soccer_teams" INNER JOIN "comments" ON "soccer_teams".id = "soccer_teams".commentable_id AND "comments".commentable_type = 'SoccerTeam' WHERE (("comments".commentable_id = 1) AND ("comments".commentable_type = 'Manager'))
For now, I want it to be:
SELECT "soccer_teams".* FROM "soccer_teams" INNER JOIN "comments" ON "soccer_teams".id = "comments".commentable_id AND "comments".commentable_type = 'SoccerTeam' WHERE ("comments".author_id = 1)
So the question is simple: how to get this request? (A heuristic attempt with :foreign_key ans :as , as expected, did not solve the problem!).
Alberto santini
source share