Monday, 15 August 2011

ruby on rails - Can I query the current scope(s)? -



ruby on rails - Can I query the current scope(s)? -

my collections model has scope this

scope :got, -> { where(status: 'got') }

i have link index follows

<%= user_collections_path(@user, got: true) %>

thanks has_scope gem creates index of user's collections status: 'got'. path

users/user1/collections?got=true

in index view want able write

<% if status: 'got' %> have these ones <% end %>

but no matter how write can't seem query scope passed in link. there standard way of doing this?

you can following:

<% if params[:got].to_s == 'true' %> have these ones <% end %>

but forces utilize true value params[:got]. maybe better:

<% if params[:got].present? %> have these ones <% end %>

but work params like:

users/user1/collections?got=true, users/user1/collections?got=false, users/user1/collections?got=nope, etc.

actually, has_scope gem provides method current_scopes returns hash (key = scope, value = value given scope) in corresponding views. should able this:

<% if current_scopes[:got].present? %> have these ones <% end %>

ruby-on-rails ruby-on-rails-4 named-scope

No comments:

Post a Comment