AnthonyGalli.com
AnthonyGalli.com

Reputation: 2866

How to remove blanks from select options?

What's a best practice to remove blanks select options?

enter image description here

form

<%= f.select :challenge_id, options_for_select(@current_user_challenges.collect { |current| [current.full_challenge, current.id] }) %>

challenges_controller

#My Attempts:
@current_user_challenges = current_user.challenges.order(:created_at).where("user_id > 0")
@current_user_challenges = current_user.challenges.order(:created_at).reject!(&:blank?)
@current_user_challenges = current_user.challenges.order(:created_at).reject { |id| id.blank? }
@current_user_challenges = current_user.challenges.order(:created_at).select!{|val| !val.empty?}

Upvotes: 0

Views: 92

Answers (1)

Deepak Mahakale
Deepak Mahakale

Reputation: 23661

You can select values with present? check

@current_user_challenges = 
  current_user.includes(:challenges).challenges.order(:created_at).select{ |challenge| challenge.full_challenge.present? }

Also, I will suggest you can add a presence validation at model level

class Challenge
  validates :full_challenge, presence: true
end

Upvotes: 2

Related Questions