Rails uniq Public Activity
Rails uniq 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" 的重复条目,因为如果用户多次单击 "update",我会得到很多条目。
所以,我只想得到其中一个。
我试过
.uniq{|a| "#{a.trackable_type}-#{a.trackable_id}"}
但什么也没发生。
已返回 SQL 查询:
"SELECT DISTINCT \"activities\".* FROM \"activities\" WHERE
(((((\"activities\".\"trackable_type\" = 'Lesson' AND
\"activities\".\"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) OR \"activities\".\"trackable_type\" = 'Post'
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
\"activities\".\"trackable_type\" = 'WallPost' AND
\"activities\".\"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)) OR
\"activities\".\"trackable_type\" = 'Comment' AND
\"activities\".\"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)) OR \"activities\".\"trackable_type\" = 'Course' AND
\"activities\".\"trackable_id\" IN (8, 1, 5, 7)) OR
\"activities\".\"trackable_type\" = 'Group' AND
\"activities\".\"trackable_id\" IN (14, 11, 4, 5, 6, 3, 2, 1)) ORDER
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")
我明白了:
更新 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"
没有 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)
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" 的重复条目,因为如果用户多次单击 "update",我会得到很多条目。
所以,我只想得到其中一个。
我试过
.uniq{|a| "#{a.trackable_type}-#{a.trackable_id}"}
但什么也没发生。
已返回 SQL 查询:
"SELECT DISTINCT \"activities\".* FROM \"activities\" WHERE (((((\"activities\".\"trackable_type\" = 'Lesson' AND \"activities\".\"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) OR \"activities\".\"trackable_type\" = 'Post' 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 \"activities\".\"trackable_type\" = 'WallPost' AND \"activities\".\"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)) OR \"activities\".\"trackable_type\" = 'Comment' AND \"activities\".\"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)) OR \"activities\".\"trackable_type\" = 'Course' AND \"activities\".\"trackable_id\" IN (8, 1, 5, 7)) OR \"activities\".\"trackable_type\" = 'Group' AND \"activities\".\"trackable_id\" IN (14, 11, 4, 5, 6, 3, 2, 1)) ORDER 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")
我明白了:
更新 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"
没有 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)