0

My view code is as below:

<%= b.input :city, as: :select, collection: Lov.where(:remarks => 'lov_city').select('id').select('lov_content'),  placeholder: 'Enter City', label: false %>

When the code is render, the list of data that is shown in my view is #<Lov:0x3241351> ..

Actually the Lov table is a reference table which is not related to any other tables and when i do in the rails console (rails c) and running the same 'Lov.where(:remarks => 'lov_city').select('id').select('lov_content')' the data is shown but the view is different.

From rails console:

jruby-1.7.16.1 :001 > Lov.where(:remarks => 'lov_city').select('id').select('lov_content')
  lovs Columns (6.0ms)  SHOW FULL COLUMNS FROM `lovs`
  Lov Load (8.0ms)  SELECT id, lov_content FROM `lovs` WHERE `lovs`.`remarks` = 'lov_city'
 => [#<Lov id: 229, lov_content: "Ayer Baloi">, #<Lov id: 230, lov_content: "Ayer Hitam">, #<Lov id: 231, lov_content: "Ayer Tawar 2">, #<Lov id: 232, lov_content: "Ayer Tawar 3">, #<Lov id: 233, lov_content: "Ayer Tawar 4">
muhammadn
  • 330
  • 3
  • 18

2 Answers2

1

If you want to fetch some selected column then use pluck

Lov.where(:remarks => 'lov_city').pluck(:id, :lov_content)
Hetal Khunti
  • 787
  • 1
  • 9
  • 23
0
Lov.where(:remarks => 'lov_city').select('id').select('lov_content')

This will return an active record object with only an id and lov_content as attribute. I am not sure of what you are trying to display but you need to provide a collection of value instead of an activerecord object.

First of all you can select several attributes at once without having to write several time select such as:

Lov.where(:remarks => 'lov_city').select([:id, :lov_content])

if you need to provide the list of lov_content then you can use this to return an array of lov_content:

Lov.where(:remarks => 'lov_city').collect(&:lov_content)
Typpex
  • 488
  • 4
  • 11
  • I had missing attribute :id once so i had selected the id. (in fact i only selected 'lov_content' only at first). I wanted to provide the list of lov_content based on the remarks (for example, it has lov_gender, lov_state, lov_marital in remarks column like lov_city - all of the items are for reference). .collect is what i needed and again thanks! – muhammadn Dec 16 '14 at 05:06
  • 1
    Better use `pluck` instead of `collect` here, see the difference http://stackoverflow.com/questions/12176102/what-is-the-difference-between-pluck-and-collect-in-rails – Nimir Dec 16 '14 at 05:07