# Problem with a selfmade helper method

**URL:** <https://www.ruby-forum.com/t/problem-with-a-selfmade-helper-method/64441>\
**Category:** Rails\
**Created:** [January 23, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/problem-with-a-selfmade-helper-method/64441 "2006-01-23T15:59:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![timo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@timo](https://www.ruby-forum.com/u/timo)\
**Post date:** [January 23, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/problem-with-a-selfmade-helper-method/64441/1 "2006-01-23T15:59:30Z")

</div>

Hi all,

i tried to set up the helper method “grouped\_programs\_selector” in  
application\_helpers.rb. Since i dont have a deep understanding of Ruby  
itself, I pretty much copied the Grouped Selection List (Chapter 17, pp.  
259, 260) example from Dave & David’s Book.  
The optiongroups are properly created by my method, but there are no  
options  
inside. Maybe you can debug this one?

-snip- from index.rhtml

\<%=  
option\_groups\_from\_collection\_for\_select(  
grouped\_programs\_selector(:programs, :programtypes), # calling  
application\_helper - method.  
:options, :type\_name,  
:id, :name,  
@programs)  
%\>

-snap- application\_helper.rb

module ApplicationHelper

```
class ProgramOption
    attr_reader :attributes
    @attributes = []
    def initialize(id, name)
        @attributes = Struct.new(@id, @name)
    end
end

class ProgramType
    attr_reader :type_name, :options
    def initialize(name)
        @type_name=name
        @options=[]
    end
    def <<(option)
        @options << option
    end
end

```

# The actual helper method

```
def grouped_programs_selector(programs, programtypes)
    groups = []
    @programtypes.each do |pt|
        newgroup = ProgramType.new(pt.name)
        @programs.each do |pr|
            if pt.id == pr.programtype
                newgroup << ProgramOption.new(pr.id, pr.name)
            end
        end
        groups << newgroup
    end
    return groups
end

```

end

---

<div class="post-metadata">

**Author:** ![timo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@timo](https://www.ruby-forum.com/u/timo)\
**Post date:** [January 23, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/problem-with-a-selfmade-helper-method/64441/2 "2006-01-23T16:15:57Z")

</div>

In your ProgramOption’s constructor, I think you have to use id and name  
instead of @id and @name

---

<div class="post-metadata">

**Author:** ![timo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@timo](https://www.ruby-forum.com/u/timo)\
**Post date:** [January 23, 2006, 4:41pm UTC](https://www.ruby-forum.com/t/problem-with-a-selfmade-helper-method/64441/3 "2006-01-23T16:41:27Z")

</div>

Thanks for your quick answer.  
Nope, that didn’t do the trick. But I found, that

if pt.id == pr.programtype

is never getting true. Strange…

---

<div class="post-metadata">

**Author:** ![timo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@timo](https://www.ruby-forum.com/u/timo)\
**Post date:** [January 23, 2006, 4:50pm UTC](https://www.ruby-forum.com/t/problem-with-a-selfmade-helper-method/64441/4 "2006-01-23T16:50:48Z")

</div>

Timo wrote:

> Thanks for your quick answer.  
> Nope, that didn’t do the trick. But I found, that
> 
> if pt.id [http://pt.id/](http://pt.id/) == pr.programtype
> 
> is never getting true. Strange…  
> That’s because you’re comparing a Fixnum to a ProgramType. Try:  
> if pt == pr.programtype
