0

Table: age_values

id | kids_age | teen_age  | old_age

1  | Age - 1  | Age - 16  | null

2  | Age - 2  | Age - 17  | Age - 25

3  | Age - 3  | Age - 18  | null

4  | Age - 4  | Age - 19  | Age - 26

5  | under 5  | Age - 20  | Age - 28

I have a table age_values and model for the table is

class AgeValue < ActiveRecord::Base
  self.table_name = 'age_values
  self.primary_key = 'id

  def to_s
    kids_age
  end

  def to_s_teen
    teen_age
  end

  def to_s_old
    old_age
  end

end

in my view,

# some_condition = 5

<% if some_condition = 1 %>
  <% age = to_s %>
<% elsif some_condition = 2 %>
  <% age = to_s_teen %>
<% else %>
  <% age = to_s_old %>
<% end %>

<%= f.collection_select(age_value_for_question, :id, age, {include_blank: ((some_variable || 0) != 1) ? 'Please select an option' : false}, {name: some_name}) %>

I want to display the option which is not null. Basically for old_age just want to display {id: 2, old_age: Age - 25, id: 4, old_age: Age - 26, id: 5, old_age: Age - 28}

What I have Tried:

I have changed and added another method in my model

def to_s_old
  exclude_null_for_old_age
end

def exclude_null_for_old_age
  age = AgeValue.where("old_age IS NOT NULL")
  age.pluck(:old_age)
end

I Want to exclude the null of value 1 and 3)

My current result:

<option value=1>["Age - 25", "Age - 26", "Age - 28"]</option>
<option value=2>["Age - 25", "Age - 26", "Age - 28"]</option>
<option value=3>["Age - 25", "Age - 26", "Age - 28"]</option>
<option value=4>["Age - 25", "Age - 26", "Age - 28"]</option>
<option value=1>["Age - 25", "Age - 26", "Age - 28"]</option>

My Expected result:

<option value=2>Age - 25</option>
<option value=4>Age - 26</option>
<option value=5>Age - 28</option>
7
  • have you tried using .compact ? Commented May 13, 2015 at 14:16
  • @cyzanfar Where? at model or in the view? I tried on both model and view, it says undefined_method 'compact' for nil values. Commented May 13, 2015 at 14:19
  • I would do it in the model Commented May 13, 2015 at 14:20
  • @cyzanfar I got undefined method 'compact' for nil:NilClass :-( Commented May 13, 2015 at 14:30
  • wha are you calling .compact on? Commented May 13, 2015 at 14:31

2 Answers 2

2

First, make the method in your AgeValue model into a scope (basically a class method, so it can be called on the class itself):

class AgeValue < ActiveRecord::Base
  scope :old_ages, -> { where.not old_age: nil }

  # ... other model stuff
end

And then use that in your view:

f.collection_select :age_value_id, AgeValue.old_ages, :id, :old_age

Done.

Update

Btw, there are many things wrong with your code that I didn't mention, but that will trip you up:

  1. Your self.table_name assignment doesn't close the single quote.
  2. Your self.primary_key assignment is the same.
  3. Neither of those are actually required, because both are the default values ActiveRecord would set anyway.
  4. <% if some_condition = 1 %> will always be true because it's an assignment (using =) not a boolean test (using ==)
  5. <% age = to_s %> to_s here is being called on the implicit self which will be some weird view instance.
  6. Even if you called it on the model (eg. @age_value.to_s) it will return you the value of kids_age not the label :kids_age so when you use it in f.collection_select as you seem to be, then Rails won't be able to call that method anyway.
  7. That doesn't even matter because collection_select expects the name of the field it's assigning to first, then the collection, then the value method and then the text/display method, so four args - you seem to have missed an arg and so nothing will be called correctly anyway.

So, in summary, this code is never going to work and has so many serious problems that I don't believe it's actually working as you've pasted it, but even with typos fixed it still has serious issues.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @smathy, But My code is bit complicated. Its already having some methods in it. Is it anywhere that we can get the results by changing the model not in the view?
My response is the update I've just made to my answer. There's really nothing I can do to help this code.
Sorry for the typos. Everything I just write it up. My code is working fine. I dont want to copy and paste the real codes. That's why I wrote everything as an example and I made lot of typos. I just need to change in the model to make this work.
That's a really bad way to ask a question here on SO, and makes it virtually impossible to help you (and extremely frustrating for people who try), please read How to create a Minimal, Complete, and Verifiable example so you can ask great questions next time. For this time, I've given you the answer to the question that you asked, my solution is how to skip null values in your collection_select.
0

If you need to be able to reset selection:

f.input :ptype, collection: PROC_TYPES, label: 'Service Type', include_blank: 'None'

Otherwise:

f.input :ptype, collection: PROC_TYPES, label: 'Service Type', include_blank: false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.