Ruby Sequel: Array Returned by Query Is Being Returned as a String Object, Not an Array Object

Ruby Sequel: Array returned by query is being returned as a String object, not an Array object

You need to use DB.extension :pg_array, :pg_inet, :pg_json, not Sequel.extension :pg_array, :pg_inet, :pg_json. Otherwise you are just requiring the files without modifying the configuration for the Sequel::Database instance.

ruby sequel gem - how to query arrays with the pg_array extension

Use the pg_array_ops extension:

Sequel.extension :pg_array_ops
Email.where(Sequel.pg_array_op(:references).contains('5363f773bccf9_32123fe75c45e6f090953@Pauls-MacBook-Pro.local.mail'))

Postgres and Ruby's Sequel: Empty array in where-clause getting converted to slow query

There is a closed issue on github about this behavior.

I agree with jeremyevans that there is no need for a fix, because it is obvious that the reseult will and should always be empty. Furthermore you would agrue that PostgreSQL should be clever enough to optimize queries like that and should not do a whole table scan.

Therefore it makes way more sense IMHO to fix this behavior in the code to avoid to call the database completely:

user_ids.present? ? User.where(id: user_ids) : []

How to select schema/table/column in Sequel using the shortcut syntax?

According to Jeremy Evans, Sequel does not support the fluent syntax to 3 levels deep.

Instead, the solution is to use lower-level methods to do so:

puts RDB[:store__albums].
select(Sequel.qualify(:store, :albums__title).
as(:album_title)).sql

Sequel format for constants, functions calls in select?

If you want a more concise way, you can use implicit aliasing using symbols with a triple underscore, use just the select method with a virtual row block:

DB[:T___t].select('0'.lit.as(:a), :t__x___c){sin(t__x).as(b)}

This isn't exactly the same as the order of the selected columns differs, but that usually doesn't matter.

How to pass a list to an IN clause via a placeholder with Ruby Sequel

query = "
SELECT *
FROM PRICE
WHERE REGION IN ?"
regions = Sequel.lit("'NSW1', 'VIC1'")
sql = db.fetch(query, regions).sql
puts "sql: #{sql}"

Gives:

sql: SELECT *
FROM PRICE
WHERE REGION IN ('NSW1', 'VIC1')

which is what we want.

Is there a way to see the raw SQL that a Sequel expression will generate?

You can call sql on dataset:

db.select(:id).from(:some_table).where(:foo => 5).sql # => "SELECT `id` FROM `some_table` WHERE (`foo` = 5)"

For update queries you can do this:

db.from(:some_table).update_sql(:foo => 5) # => "UPDATE `some_table` SET `foo` = 5"

Some similar useful methods:

insert_sql
delete_sql
truncate_sql


Related Topics



Leave a reply



Submit