如何根据条件在两个作用域之间使用一个作用域



我有一个模型POSTS。 因为我在这个模型中有两个作用域 activeinactive . 我想根据params[:active]PostsController使用其中一个。 喜欢关注

if params[:active] == "true"
 @posts = POSTS.where some_condition
       .include some_thing
       .active
       .page
       .per
else
  @posts = POSTS.where some_condition
       .include
       .inactive
       .page
       .per
 end

有没有更好的方法来做同样的事情?我尝试了很多方法。 但我做不到。帮帮我。谢谢

@posts = POSTS.where some_condition
              .include some_thing
              .public_send(params[:active] == "true" ? :active : :inactive)
              .page
              .per

Object#public_send .

最新更新