代码之家  ›  专栏  ›  技术社区  ›  frankodwyer

如何找到以acts_为应课差饷的未评分模型

  •  0
  • frankodwyer  · 技术社区  · 17 年前

    我有一个rails应用程序使用 acts_as_rateable 插件。

    我一直在琢磨如何使用这个插件检索未评级的模型——然而,这更像是一个一般的rails/SQL问题,而不是这个插件特有的问题。

    作为应课差饷缴纳人将以下内容添加到架构中:

    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"
    

    我的分级型号也有一个用户id列。

    我希望能够检索到一个特定模型的所有实例,这些实例根本没有被评分过,也没有被模型创建者以外的人评分过,例如,这里有一个模型:

    class Review < ActiveRecord::Base
      acts_as_rateable
    
      belongs_to :user
      ...
    end
    

    所以我想要下面的伪代码

    Review.find(:all, :conditions=>"not rated by anyone")
    Review.find(:all, :conditions=>"not rated by anyone except review.user")
    

    然而,我不知道如何使用SQL来实现这一点,也不知道rails如何神奇地生成SQL:-)

    更新:此查询似乎会查找除拥有该模型的用户之外的其他人对其进行评级的所有模型。所以我想我只需要以某种方式把它颠倒过来。

     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')
    
    2 回复  |  直到 17 年前
        1
  •  1
  •   John Topley    17 年前

    命名作用域是解决这个问题的方法。我会在你的 Review 模型比如:

    class Review < ActiveRecord::Base
      acts_as_rateable
    
      belongs_to :user
    
      named_scope :not_rated, :conditions => { :rating => 0 }
      named_scope :not_rated_by_others,
                  :conditions => ["user != ? AND rating == 0", self.user]
    end
    

    然后你可以做:

    @not_rated = Review.not_rated
    @not_rated_by_others = Review.not_rated_by_others
    

    有一个 Railscast that explains named scopes .


    编辑:第二次尝试

    好吧,我们再来一次!其中一个问题是存在多个问题 acts_as_rateable 外挂程序。我一直在用 this one 在RubyForge上。

    class Review < ActiveRecord::Base
      belongs_to :user
    
      acts_as_rateable
    
      named_scope :not_rated, :select => 'reviews.*',
                  :joins => 'left join ratings on reviews.id = ratings.rateable_id',
                  :conditions => 'ratings.rateable_id is null'
    
      named_scope :not_rated_by_others, lambda { |user| { :select => 'reviews.*',
                  :joins => 'left join ratings on reviews.id = ratings.rateable_id',
                  :conditions => ['ratings.user_id != ? and ratings.rateable_id is null',
                     user] }}
    end
    

    这样使用:

    frank = User.find_by_name('frank')
    @not_rated = Review.not_rated
    @not_rated_by_others = Review.not_rated_by_others(frank)
    
        2
  •  0
  •   frankodwyer    17 年前

    我有一些方法可以使用这样的查询:

    Review.find(:all, 
     :joins=>'left outer join ratings on reviews.id=ratings.rateable_id && ratings.rateable_type="Review"',
      :conditions=>'ratings.rating is NULL')
    

    这看起来像是返回了所有没有评级的审查模型。

    我认为这是为了找到所有没有被创建评论的用户评分的评论模型:

    Review.find(:all, 
      :joins=>'left outer join ratings on reviews.id=ratings.rateable_id && ratings.rateable_type="Review" && ratings.user_id <> reviews.user_id',
      :conditions=>'ratings.rating is NULL')