It would probably at that.  Might be better to start thinking about
keeping counts at least cached if not fully handled.

The only thing I worry about is the chance that I or hobo have some
update calls in place that wouldn't trigger an observer.

I worked out a way to do it with SQL but means I have to add an
additional layer of SQL for every ratable model.

    raise "Invalid object(s) passed.\n#{user.inspect}\n#
{category.inspect}" unless
        user.class == User or category.class == RatingCategory
    Rating.connection.select_value("SELECT SUM(rating) FROM " +
      "(SELECT DISTINCT ratings.* FROM ratings,pictures,users where "
+
      "(ratable_id = pictures.id AND pictures.user_id = #{user.id}) "
+
      "and rating_category_id = #{category.id});").to_i

Unfortunately, select_value doesn't do SQL escaping which is somewhat
disconcerting.

On Aug 6, 9:32 pm, Bryan M <[email protected]> wrote:
> Why not add two fields to User, num_ratings and avg_rating, and update
> User with a RatingObserver?
>
> (I hope I'm not talking out my arse, but I think that would work)
>
> On Aug 6, 7:46 pm, kevinpfromnm <[email protected]> wrote:
>
> > Ok, I'll start off with the basic models
> > Rating
> >   belongs_to :ratable, :polymorphic => true
>
> > Picture
> >   has_many :ratings, :as => :ratable
> >   belongs_to :user, :creator => true
>
> > Post
> >   has_many :ratings, :as => :ratable
> >   belongs_to :user, :creator => true
>
> > I'm wanting to be able to give an average for a user for all ratings
> > applied to their submissions.  There will be additional models in the
> > future that are ratable as well.
>
> > So, any ideas on a good way to approach this?  There are a couple of
> > simple ways but they won't scale.  And this relation will probably be
> > one of the most pushed that I'll have in the app.  So, I really need a
> > way to grab the rating for all ratables that belong to a user.  From
> > my current sense of things looks like I'm going to have to write some
> > SQL to do it but figured I'd ask anyway in case someone knew of a nice
> > trick to do sums across polymorphic types.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/hobousers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to