0

I'm trying to make a collection of items for options_for_select using map inside my model:

  def get_products
    Product.all.map { |product| [product.description,product.id] }
  end

However rails is giving the error:

undefined method `map' for :get_products:Symbol

Is not possible to use map inside models?

The code in the view is:

<%= f.select_tag :product_id, options_for_select(:get_products) %>

Update

Changing the code in the view to get_products instead of :get_products gives the error:

undefined local variable or method `get_products' for #<#:0xb189c770>

2
  • 1
    May we see the line where you are calling options_for_select? Commented Nov 29, 2012 at 20:59
  • 1
    All of the above works fine (returns an array of arrays); chances are it's how you reference it in the options_for_select that's causing the problem -- perhaps it should be get_products instead of :get_products? Commented Nov 29, 2012 at 21:01

2 Answers 2

3

options_for_select expects a collection to be passed in and not a symbol. I'm fairly certain your error is coming from inside the Rails code and not your model.

Change #get_products to be a class method:

def self.get_products
  Product.all.map { |product| [product.description,product.id] }
end

And then in the view (assuming #get_products lives within a model called Product):

<%= f.select :product_id, options_for_select(Product.get_products) %>
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much, but shame on me there is no method select_tag for form_for. What should i do?
@EH_warch I updated my answer. I don't think you need _tag just f.select
0

It should be something like this

<%= f.select_tag :product_id, options_for_select(Product.get_products) %>

2 Comments

Without adding the self to the method inside the model did not work
Yeah sorry thought you already had that, but I guess it was from @patricmcgraw's response

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.