Skip to content

Instantly share code, notes, and snippets.

@akhtarja

akhtarja/1.sql Secret

Last active June 1, 2020 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhtarja/ed71ae4caa99793e0e7ce512d1482a39 to your computer and use it in GitHub Desktop.
Save akhtarja/ed71ae4caa99793e0e7ce512d1482a39 to your computer and use it in GitHub Desktop.
Blog post 4 - Returning a JSON Field From Rails GraphQL
select
items.id as ItemId,
items.name as ItemName,
items.price as ItemPrice
from items
where items.id = 12
query = %(
select
items.id as ItemId,
items.name as ItemName,
items.price as ItemPrice
from items
where items.id = 12
)
query_results = ActiveRecord::Base.send(:sanitize_sql_array, query)
query_results = ActiveRecord::Base.connection.select_all(query_results).rows
query_results #=> [ [12, "My item", 4.99] ]
query = %(
select
items.id as ItemId,
items.name as ItemName,
items.price as ItemPrice
from items
where items.id = #{item_id}
)
query = %(
select
items.id as ItemId,
items.name as ItemName,
items.price as ItemPrice
from items
where items.id = ?
)
query_results = ActiveRecord::Base.send(:sanitize_sql_array, [query, item_id])
query_results = ActiveRecord::Base.connection.select_all(query_results).rows
select
items.id as ItemId,
items.name as ItemName,
items.price as ItemPrice
from items
where items.price > 3
and items.name ~ 'item'
query = %(
select
items.id as ItemId,
items.name as ItemName,
items.price as ItemPrice
from items
where items.price > :item_price
and items.name ~ :item_name
)
ActiveRecord::Base.send(:sanitize_sql_array, [query, { item_price: 3, item_name: 'item' }])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment