Rails Chained或条件在哪里

时间:2015-05-07 12:28:06

标签: ruby-on-rails ruby-on-rails-4 activerecord

我必须在Active Record中执行此查询(squeel)。 我的{@my_lessons_ids} 是一个包含所有IDS的数组 >> 代表“WHERE IN” | 代表“或”

    my_courses = current_user.courses

@my_posts_ids   = current_user.enrollments.map{|e| e.academy.posts.map(&:id)}
@my_courses_ids = my_courses.map(&:id)
@my_groups_ids  = current_user.groups.map(&:id)
@my_lessons_ids = my_courses.map{|c| c.lessons.map(&:id)}

@my_wallposts = WallPost.where{
    ((wallable_type == "Lesson") && (wallable_id >> my{@my_lessons_ids})) |
    ((wallable_type == "Post") && (wallable_id >> my{@my_posts_ids})) |
    ((wallable_type == "Course") && (wallable_id >> my{@my_courses_ids})) |
    ((wallable_type == "Group") && (wallable_id >> my{@my_groups_ids})) 

}

@my_comments_ids = @my_wallposts.map{|w| w.comments.map(&:id)}
@my_wallposts_ids = @my_wallposts.map(&:id)

@activities = PublicActivity::Activity.where{
    ((trackable_type == "Lesson") & (trackable_id >> my{@my_lessons_ids})) | 
    ((trackable_type == "Post") & (trackable_id >> my{@my_posts_ids}))       |
    ((trackable_type == "Course") & (trackable_id >> my{@my_courses_ids}))   |
    ((trackable_type == "WallPost") & (trackable_id >> my{@my_wallposts_ids})) |
    ((trackable_type == "Comment") & (trackable_id >> my{@my_comments_ids})) 

}.order("id DESC")

我不能使用“squeel”,因为它会与其他东西产生一些冲突

1 个答案:

答案 0 :(得分:1)

您可以使用ARel

content == text