user3166642 user3166642 - 3 months ago 10
Ruby Question

Converting an ActiveRecord:Relation to java array

I am running a 'where' query which is running on a table MyTable in my rails application.
I want to convert the results of a specific column from this query(ActiveRecord::Relation) to Java Array of String type.

This is what I am doing :

employeesJavaArray=MyTable.where("salary = ?",100).pluck(:columnName).to_java(java.lang.String)


However I am receiving this error in my logs :-

TypeError (could not coerce Fixnum to class java.lang.String):


Can you please help me out what could be wrong with the statement that I have written.

Answer

I would ensure that the array only includes string (by calling to_s) first:

employeesJavaArray = MyTable.where("salary = ?",100)
                            .pluck(:columnName)
                            .map(&:to_s)
                            .to_java(java.lang.String)