TidusLe
TidusLe

Reputation: 41

Cannot get information in mysql result with rails

I'm using Rails with ActiveAdmin gem. And I want to select some information from mysql database.

sql = ActiveRecord::Base.connection();
s="SELECT word FROM dics WHERE word LIKE 'tung%'";
ten = sql.execute(s);

But when I printed out "ten" to screen, it showed that:

#<Mysql2::Result:0x4936260>

How can I get the information of records?

Upvotes: 0

Views: 167

Answers (1)

oldhomemovie
oldhomemovie

Reputation: 15129

I suggest that you don't use ActiveRecord::Base.connection directly. Sticking with ARel syntax should work for most cases, and your example doesn't seem like an edge case.

As stated in the comments above, try the following:

dics = Dic.select(:word).where(["word LIKE ?", "tung%"]).all

In order to pluck some special field of object, not objects themselves, use pluck instead of all:

# instead of .pluck(:word) use real field identifier
dics = Dic.where(["word LIKE ?", "tung%"]).pluck(:word)

Upvotes: 1

Related Questions