C
C
cnaize2014-04-25 15:08:46
PostgreSQL
cnaize, 2014-04-25 15:08:46

Rails calling Postgresql method?

I need to call a postgres method from rails.

categories = Entity.joins(:language, :category).
        select("coalesce('entities.short_descr', 'categories.short_descr')").
        where('languages.code = ?', 'en')

render json: {status: 'ok', categories: categories}

it doesn't work, returns
{"status":"ok","categories":[{"coalesce":"entities.short_descr","id":null}]}

How to do it right?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Arkady Butermanov, 2014-04-25
@Arkadey

Find By SQL ?

I
Ivan Kryak, 2014-04-26
@sck_v

....select("coalesce('entities.short_descr', 'categories.short_descr') as short_descr, entities.* ")....

D
Dimitriy, 2014-05-27
@Vakiliy

Maybe so?

t1, t2 = [Entity.arel_table .....
a = Arel::Nodes::NamedFunction.new('coalesce', [t1[:short_descr], t2[:short_descr]]).as('descr')
Entity.select(a).....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question