Narasimha Reddy - Geeker
Narasimha Reddy - Geeker

Reputation: 3880

how use one scope between two based on a condition?

I have a model POSTS. in that i have two scopes in this model active, inactive. i want use one of it PostsController based on params[:active] value. like following

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

is there any better way to do the same thing? i tried many ways to do it. but i couldn't. help me. thanks

Upvotes: 0

Views: 44

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

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

Object#public_send.

Upvotes: 4

Related Questions