I need to create a method which can be called so
list_of_hash_values = MyModel.values_and_links :title, "some_query_param", lambda{|id| url_for :controller => :my_model, :action => :show, :id=> id }
It gets 3 arguments: field name (as a symbol), query parameter and lambda with one parameter. And it returns hash of values and their urls. Lambda return some string value (in this case it is ulr of a model instance).
I tried to do:
class MyModel
def values_and_links(title, query_param, lambda_exp)
instance_values = MyModel.order(:title).where("#{title} = ?", "%#{query_param}%")
instance_values.each do |item|
{:hash_key1 => item.send(title), :hash_key2 => lambda_exp.call(title)}
end
end
end
It’s wrong, not working.
I hope you understand what I need to accomplish. How can I do it? How can I implement method values_and_links?
I would use a block instead:
then, you can call your method like this:
or, for ‘short’: