I have a rails application using the acts_as_rateable
plugin.
I'm stuck on figuring out how to retrieve unrated models using this plugin - however this is more of a general rails/SQL question than specific to this plugin.
Acts as rateable adds the following to the schema:
create_table "ratings", :force => true do |t|
t.integer "rating", :default => 0
t.string "rateable_type", :limit => 15, :default => "", :null => false
t.integer "rateable_id", :default => 0, :null => false
t.integer "user_id", :default => 0, :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "ratings", ["user_id"], :name => "fk_ratings_user"
And my rated models also have a user id column.
I'd like to be able to retrieve all instances of a particular model that haven't been rated at all, and also all instances that haven't been rated by someone other than the creator of the model, e.g. here is one model:
class Review < ActiveRecord::Base
acts_as_rateable
belongs_to :user
...
end
So I want something like the following pseudocode
Review.find(:all, :conditions=>"not rated by anyone")
Review.find(:all, :conditions=>"not rated by anyone except review.user")
However I can't figure out the SQL to do this, nor the rails magic to generate that SQL :-)
Update: this query seems to find all models that ARE rated by somebody other than the user that owns the model. So I think I just need to invert this somehow.
Review.find(:all,
:joins=>'left join ratings on reviews.id=ratings.rateable_id && ratings.rateable_type="Review"',
:conditions=>'reviews.user_id <> ratings.user_id',
:group=>'reviews.id')