When storing floating-point numbers such as prices or totals in an SQL database, always use a DECIMAL column. Never use FLOAT or kittens will die. DECIMAL columns are parametrized...
...some caching for you. There, it's perfectly fine as this is actually an SQL query (that you wouldn't want to do multiple times) and the association is actually...
...PostgreSQL dumps. This is no instruction for a backup strategy nor a guide for SQL dump performance optimization. Read before starting I will assume that all commands will be executed...
...should this not be an instance method? You can implement the search in either SQL or Ruby The form_for helper is not helpful for the search form above the...
...s Common Table Expressions (CTEs) can be used to extract sub-queries from bulky SQL statements into a temporary table to be referenced instead. This is most useful to avoid...
...in the rails console and it will log the exact database interaction with related sql commands and its result. Before you know where to debug, you will often find yourself...
...and what tools Rails gives you to address it. Cross-Site Request Forgery (CSRF) SQL Injection Cross-Site Scripting (XSS) Content Security Policy Also A reasonable default Content Security Policy...
...You are submitting queries to fetch data in a given structure (like SELECT in SQL) or mutations to alter the database (similar to POST/PUT/DELETE in REST). You can control how...
...string argument as the value for ‘column’. This string is passed directly to the SQL statement, making it possible to use all sorts of fun things to lock down the...
So regarding to the cited site Rails 5+ allows me to use an SQL statement in my index: t.index 'shop_id, lower(street), lower(house_number), lower(zip_code...
...many thousand keys). This means every association that you pivot around will trigger one SQL query...
...example can also be achieved with Rails internals: Article.group(:brand).count. This translates to SQL, so it executes fast. However, grouping is restricted to columns (attributes). Using #count_by gives...
When you want to filter records in a model where a string column roughly matches a given term, you can...
...column delegates to #pluck, which can be used for the same effect. Relation#to_sql # Rails 2 scope Post.scoped(:conditions => { :id => [1, 2] }).to_sql # => "SELECT `posts`.* FROM `posts` WHERE...
...posts.id` IN (1, 2)" ^ # Rails 3 relation Post.where(:id => [1, 2]).to_sql # => "SELECT `posts`.* FROM `posts` WHERE `posts.id` IN (1, 2)" Implementation note: Rails 3+ implements #to_sql.
...okay, but next automatic id will still be 101 Record.create! # will cause an SQL error 'duplicate key value violates unique constraint "records_pkey"' Workaround In tests, one workaround would be...
...With a Database Dump? Unfortunately not. Restoring from a logical database backup, i.e. an SQL dump, will not reproduce the error and is not suitable for testing changes, since importing...
...on any index oid # then it will drop the amcheck extension again list_index_sql=" SELECT c.oid FROM pg_index i JOIN pg_opclass op ON i.indclass[0] = op.oid
The change_column method for rails migrations support casting with a custom SQL statement. This allows us to change a column type and keep the former content as the new...
...are consuming the most disk space. You can easily check this using the following SQL statement from the PostgreSQL wiki. SELECT nspname || '.' || relname AS "relation", pg_size_pretty(pg_total...
...fallback to ActiveRecord::Base as connection base in the strategies that do not use SQL. That's why in the code example above, we could use DatabaseCleaner[:active_record].clean...
...you might load an outdated database structure. Since the schema.rb does not hold any SQL specific information, like charsets or collations, your test database may differ from your development database...
...without further information). To avoid this problem, your environment.rb needs to say: config.active_record.schema_format = :sql This will make a db/development_structure.sql appear which is then also read and loaded when you...
Using Scenic, you can bring the power of SQL views to your Rails application without having to switch your schema format to SQL. Scenic provides a convention for versioning views...
...that keeps your migration history consistent and reversible and avoids having to duplicate SQL strings across migrations. As an added bonus, you define the structure of your view in a...
...read more about window functions here, but for our purposes, know that the following SQL does what we need: SELECT posts.*, COUNT(*) OVER() AS full_count FROM (/* some complicated subquery...
This may be awkward to set up, but will work once you're done. Fun facts:
There are times when you need to send SQL to the database, like this: def self.some_count(field) field = connection.quote_column_name(field) scoped(:select => "COUNT(DISTINCT #{field}) AS count...
...not helping against injection: Klass.some_count("id`); DELETE FROM users; -- ") # Will result in this SQL which is valid but definitely undesirable: SELECT COUNT(DISTINCT `id`); DELETE FROM users; -- `) AS count...
Here is a way to create a duplicate of one database, with all its tables and their data, under a...