User-defined Order in SQL
The attached article Show archive.org snapshot explains options you have to store the order of items in a database table.
The simplest solution of course is to use a position
column. However the author explores some alternatives where you don't need to update multiple rows when you move a single item.
Related cards:
ActiveRecord: Order a scope by descending value without writing SQL
Instead of this:
Image.order('images.created_at DESC')
You can write this:
Image.order(created_at: :desc)
Not only do you not have to write SQL, you also get qualified column names (created_at
becomes images.created_at
) for...
Order in which RSpec processes .rb files
Because your examples should not change global state, you should not need to care about the order in which RSpec processes your .rb
files. However, in [some cases](https://web.archive.org/web/20221127190343/https://relishapp.com/rspec/rspec-co...
Order for SELECT ... IN (5,100,23) queries
When doing a query like this:
SELECT id FROM users
WHERE (users
.id
IN (899,1084,1095,100,2424,2429,2420))
the order of the returned records is undefined. To force the query to return the records in a given order, you have to add `ORDER ...
Howto use ActiveRecord preload with plain SQL inner joins
Like you know from "How to tell ActiveRecord how to preload associations (either JOINs or separate queries)", you can tell ActiveRecord explicitly if it should use a LEFT OUTER JOIN
or a separate query to preload associations....
How to explain SQL statements via ActiveRecord
ActiveRecord offers an explain
method similar to using EXPLAIN
SQL statements on the database.
However, this approach will explain all queries for the given scope which may include joins
or includes
.
Output will resemble your database's ...
Understanding SQL compatibility modes in MySQL and MariaDB
MySQL and MariaDB have an SQL mode setting which changes how MySQL behaves.
The SQL mode value is comprised of multiple flags like "STRICT_TRANS_TABLES, NO_ZERO_IN_DATE"
. Each flag activa...
Understanding race conditions with duplicate unique keys in Rails
validates_uniqueness_of
is not sufficient to ensure the uniqueness of a value. The reason for this is that in production, multiple worker processes can cause race conditions:
- Two concurrent requests try to create a user with the same name (a...
Preventing users from uploading malicious content
When you allow file uploads in your app, a user might upload content that hurts other users.
Our primary concern here is users uploading .html
or .svg
files that can run JavaScript and [possibly hijack another user's session](#section-att...
Rails SQL Injection Examples
This page lists many query methods and options in ActiveRecord which do not sanitize raw SQL arguments and are not intended to be called with unsafe user input. Careless use of these methods can open up code to SQL Injection exploits. The examples...
Debug your Postgres SQL query plan
When debugging slow SQL queries, it’s helpful to understand the database engine's query plan. Whenever you execute a declarative SQL query, the database generates a "query plan" that outlines the exact steps the engine will take to execute the que...