ruby-on-railsrubyelasticsearchjbuilder

Set key dynamically using jBuilder Elasticsearch with Rails


I want to enable the user to search using different fields from my model, for instance, a different field than name:

json.set!(:match, name: @term)

Is there a way to supply a value (string) instead of name: on the line above?


Solution

  • I am not sure I got your question, if you have instance vairable @key_value, it can be name or body or other attribute name. You could do the following:

    json.set!(:match, @key_value => @term)