Rails helper method for generating array

  Kiến thức lập trình

The following method defined for a class

  def name_with_group
    "#{self.name} #{self.foodgroup.try(:categoryminor_name)} #{self.beveragegroup.try(:categoryminor_name)}"
  end

when tested in the console returns:

Component.last.name_with_group
"Springfresh 150  mineral_water_gas"

But attempting to use that helper as a method for a collection fails

@component_names = []
@components.each do |component|
  @component_names << component.name_with_group
end

undefined method `name_with_group' for an instance of Component (NoMethodError)

which is a bit tad odd as the method has just been executed.
Why is this happening?
How should this be cast instead?

LEAVE A COMMENT