I'm using ActiveRecord to run queries on several different databases. In one of the queries, I have to do the following:
publications_to_read = [1,2,3,4]
ActiveRecord::Base.connection.execute("ALTER TABLE publications SET readed = TRUE WHERE id IN (#{publications_to_read});")
If I were doing this from a class inheriting from ActiveRecord::Base
, there would be a much more intuitive way of doing this, but that is not the case.
What I need is to interpolate this array in the query so that the end result looks like this:
publications_to_read = [1,2,3,4]
# Query usando o array adaptado
ActiveRecord::Base.connection.execute("ALTER TABLE publications SET readed = TRUE WHERE id IN ('1', '2', '3', '4');")
Can anyone tell me how I could achieve this result? I already tried to do something like publications_to_read.join(',')
but it did not work. The array is converted to a single String and gives error.