postgresql - How to use dynamic array of ids in raw query (rails + postgres)? -


i have code

language.all.map{|l|[l.language, l.accounts.joins(:children).where("accounts.id in (?)", @accounts.ids).uniq.count]} 

i trying output

[["eng", 5] ["span", 3] ["ital", 4] ... ] 

i want write raw query.

i tried this,

activerecord::base.connection.execute("select languages.language, (select count(*) accounts accounts.language_id = languages.id) languages").values 

but need pass accounts.ids dynamic.. this

select languages.language, (select count(*) accounts accounts.language_id = languages.id , (accounts.id in (#{@accounts.ids})) languages" 

when tried pass accounts.id in #{@accounts.ids} getting error (accounts.id in ([2839, 284.. .. should have been in (2839, 284..) instead, taking array.

how pass dynamically ?

you can try:

"... accounts.id in (#{@accounts.ids.join(',')})" 

hope helps.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -