Rails uniq公共活动

时间:2015-05-23 11:00:57

标签: ruby-on-rails postgresql activerecord rails-activerecord public-activity

t = PublicActivity::Activity.arel_table

@activities = PublicActivity::Activity.where(
    (t[:trackable_type].eq("Lesson").and(t[:trackable_id].in(@my_lessons_ids)))
    .or(t[:trackable_type].eq("Post").and(t[:trackable_id].in(@my_posts_ids)))
    .or(t[:trackable_type].eq("WallPost").and(t[:trackable_id].in(@my_wallposts_ids)))
    .or(t[:trackable_type].eq("Comment").and(t[:trackable_id].in(@my_comments_ids)))
    .or(t[:trackable_type].eq("Course").and(t[:trackable_id].in(@my_courses_ids)))
    .or(t[:trackable_type].eq("Group").and(t[:trackable_id].in(@my_groups_ids)))
).order("id DESC").uniq{|a| "#{a.trackable_type}-#{a.trackable_id}"}

在我的数据库中,我有“Trackable_type”+“trackable_id”的重复条目,因为如果用户多次点击“更新”,我会收到很多条目。 所以,我只想得到其中一个。 我试过了 .uniq{|a| "#{a.trackable_type}-#{a.trackable_id}"}

但没有任何反应。

返回SQL QUERY:

  

“SELECT DISTINCT”活动\“。* FROM \”activities \“WHERE   (((((\“activities \”。\“trackable_type \”='Lesson'AND   \“活动\”。\“trackable_id \”IN(11,39,40,16,5,3,12,9,13,   19,18,37,15,23,24,29,20,10,25,26,27,17,28,22,21,30,   33,35,34,36,32,31)或“活动”。\“trackable_type \”='发布'   AND \“activities \”。\“trackable_id \”IN(50,49,48,47,46,45,44,   43,42,41,40,37,36,35,34,33,32,31,30,29,28,27,26,25,   24,23,22,21,20,19,18,17,14,13,12,10,5))OR   \“活动\”。\“trackable_type \”='WallPost'AND   \“活动\”。\“trackable_id \”IN(158,157,155,154,153,152,151,   132,131,130,129,128,127,126,125,124,119,118,117,116,115,   114,113,111,110,109,108,107,106,105,104,103,102,101,100,   99,98,93,92,91,90,87,85,84,83,82,81,80,79,78,77,76,   75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,   58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,   41,39,38,37,36,35,34,33,31,30,29,28,27,26,25,24,22,   20,19,17,16,15,14,13,10,7,6,5,4))或   \“活动\”。\“trackable_type \”='评论'和   \“活动\”。\“trackable_id \”IN(100,99,98,95,83,82,81,79,   78,71,70,69,68,67,65,63,62,61,60,59,58,57,56,55,52,   51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,   34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,   17,16,15,14))或“活动”。\“trackable_type \”='课程'和   \“活动\”。\“trackable_id \”IN(8,1,5,7))或   \“活动\”。\“trackable_type \”='组'和   \“活动\”。\“trackable_id \”IN(14,11,4,5,6,3,2,1))令   BY id DESC“

更新

如果我尝试:

@activities = PublicActivity::Activity.select("distinct trackable_type, trackable_id").where(
        (t[:trackable_type].eq("Lesson").and(t[:trackable_id].in(@my_lessons_ids)))
        .or(t[:trackable_type].eq("Post").and(t[:trackable_id].in(@my_posts_ids)))
        .or(t[:trackable_type].eq("WallPost").and(t[:trackable_id].in(@my_wallposts_ids)))
        .or(t[:trackable_type].eq("Comment").and(t[:trackable_id].in(@my_comments_ids)))
        .or(t[:trackable_type].eq("Course").and(t[:trackable_id].in(@my_courses_ids)))
        .or(t[:trackable_type].eq("Group").and(t[:trackable_id].in(@my_groups_ids)))
    ).order("id DESC")

我明白了: enter image description here

更新2

如果我尝试

@activities = PublicActivity::Activity.select("DISTINCT ON(trackable_type, trackable_id) *")

我得到了

PG::SyntaxError: ERROR: syntax error at or near "ON" LINE 1: SELECT COUNT(DISTINCT ON(trackable_type, trackable_id) *) FR... ^ : SELECT COUNT(DISTINCT ON(trackable_type, trackable_id) *) FROM "activities"

1 个答案:

答案 0 :(得分:6)

没有arel的查询将是:

PublicActivity::Activity.select("DISTINCT ON(trackable_type, trackable_id) *")
              .where("(trackable_type = ? and trackable_id IN (?))
                      or (trackable_type = ? and trackable_id IN (?))",
                      "Lesson", @my_lessons_ids,
                      "Post", @my_post_ids)