Rails attribute value returned in hexadecimal

576 Views Asked by At

I am running the following active record query

Keyword.joins(:questions, :users, :associations).select("questions.*, keywords.keyword").where(users: {id: @user.id})

and presenting the results using wice_grid using the following code -

<%= grid(@questions, show_filters: :false) do |g|
  g.column name: 'ID', attribute: 'id', filter: false
  g.column name: 'Question', model:'Question', attribute: 'question'
  g.column name: 'Keyword', model:'Keyword', attribute: 'keyword'
end %>

The question attribute gets populated fine, but the keyword attribute shows up in hexadecimal value such as #< Keyword:0x00000104eb00a0>. Any idea why? Any help is highly appreciated.

2

There are 2 best solutions below

3
On BEST ANSWER

From https://github.com/leikind/wice_grid#queries-with-join-tables:

Please note that the blockless definition of the column only works with columns from the main table and it won’t work with columns with :model

<%= grid(@questions, show_filters: :false) do |g|
  g.column name: 'ID', attribute: 'id', filter: false
  g.column name: 'Question', model:'Question', attribute: 'question'
  g.column name: 'Keyword', model:'Keyword', attribute: 'keyword' do |question|
    question.keyword.keyword
  end
end %>
1
On

Are you sure you should be using a <%= instead of a <% tag? That displays the return value of any function, so if grid is returning the keyword object, that would be what is getting displayed.