Exclude Option from Collection.Map in Ruby on Rails

Exclude option from collection.map in Ruby on Rails?

map doesn't allow to skip values. You have to reject unwanted elements first.

states.reject { |s| s.name == "excluded_state" }.map { |s| [s.name, s.id] }

Another (dirtier) solution is to return nil for excluded elements and use Array#compact on the resulting array to remove those nil elements:

states.map { |s| s.name == "excluded_state" ? nil : [s.name, s.id] }.compact

How to exclude a collection of records with ActiveRecord?

You could do that:

= select :client, :id, User.where("users.id NOT IN (?)", current_user.manager_users.pluck(:client_id)).map {|u| [u.username, u.id]}, include_blank: "Add a client by username"

The new stuff is here:

User.where("users.id NOT IN (?)", current_user.manager_users.pluck(:client_id))
.map{ |u| [u.username, u.id] }

The current_user.manager_users.pluck(:client_id) part will retrieve (only on the DB-level) all the client_IDs of the manager_users linked to the current_user.

Hope this helps!

How to exclude a certain selection from a collection_select list?

You might pass to collection_select something like

@colleagues.reject {|user| user.id == current_user.id }

ruby-on-rails collection_select exclude some selections

The third param for collection_select is the collection. Instead of having this use Exchange.order(:id), you could have it apply some logic based on the company id. As an example:

class Exchange < ActiveRecord::Base
def self.not_listed_for_company_id(company)
# AR scope that retrieves all exchanges that the company is NOT listed on
# NOTE: This can be made more efficient since company.exchanges will result in additional database queries
Exchange.where("id not in (?)", company.exchanges.collect{|e| e.id})
end
end

Then, in the view:

<%= content_tag :td, f.collection_select(:exchange_id, Exchange.not_listed_for_company_id(company), 
:id, :name, {}, {class: "input-fullwidth"}), class: 'data small_column' %>

Rails4 - How to drop an option from select based on the value?

Use where not and exclude level with id 1

<%= select_tag :langlevel, options_for_select(Level.where.not(:id => 1).order(:name).map {|item| [item.name, item.id]}), id: 'langlevel_id', :class => 'form-control lang' %>

Its probably cleaner to move this logic to helper

def non_native_levels_as_options
Level.where.not(:id => 1).order(:name).map {|item| [item.name, item.id]}
end

<%= select_tag :langlevel, options_for_select(non_native_levels_as_options), id: 'langlevel_id', :class => 'form-control lang' %>

Filter to exclude elements from array

Ruby lets you use public instance methods on two arrays to get their intersecting or exclusive elements:

a1 = ['alpha', 'bravo', 'charlie', 'delta', 'echo']
a2 = ['charlie', 'echo']
puts a1 - a2
=> ['alpha', 'bravo', 'delta']

puts a1 & a2
=> ['charlie', 'echo']

For more information check rubydoc Array. It's likely that you'll find exactly what you need there.

How to exclude an array of id's from a find-method?

To find products with id not in array, you must do this:

array = current_user.products.pluck(:id)
Product.where('id NOT IN (?)', array) # '?' is surrounded by paranthesis

But since a product belongs to a user, you can simply do

Product.where('user_id != ?', current_user.id)

You can also use the not method like so:

Product.where.not(id: array)
Product.where.not(user_id: current_user.id)

Edit:

If you want the base products to be from some other list, this can help:

base_product_ids = some_list.map(&:id) # assuming this is an Array
Product.where(id: base_product_ids).where.not(user_id: current_user.id)

If some_list was an ActiveRecord::Relation, you could have simply done:

some_list.where.not(user_id: current_user.id)

Hide a Collection_Select Option

Instead of Category.all, you can pass in

Category.where("id NOT IN (?)", [1, 2, 3])

That will exclude categories with ids 1, 2 and 3.

Options for select using map and a custom option

You have to add ["All", 0] with your options.

Try like this:

<%= select_tag "group_id", options_for_select(@monitor_groups.map{|mg|[mg.name, mg.id] } + ["All", 0]) %>

Or

<%= select_tag "group_id", options_for_select(@monitor_groups.map{|mg|[mg.name, mg.id] }.push(["All", 0])) %>

Explanation:

@monitor_groups.map{|mg|[mg.name, mg.id] } 

will return an array then add ["All", 0] with your returned array.



Related Topics



Leave a reply



Submit