【问题标题】:how use one scope between two based on a condition?如何根据条件在两个范围之间使用一个范围?
【发布时间】:2016-03-09 09:00:18
【问题描述】:

我有一个模特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

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

【问题讨论】:

    标签: ruby-on-rails ruby mongodb


    【解决方案1】:
    @posts = POSTS.where some_condition
                  .include some_thing
                  .public_send(params[:active] == "true" ? :active : :inactive)
                  .page
                  .per
    

    Object#public_send.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2013-10-07
      • 2013-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多