%r(/system.*), %r(/assets.*), ] Rack::MiniProfiler.config.html_container = 'html' # avoid unpoly replacements end end SQL Rack-Mini-Profiler will give you a lot of detailed information about SQL queries happening...

If you have slow views (Haml/Partials can be slow), consider caching them. Remove code & sql-queries that are not needed to render the page. Calling to_a blindly on...

makandra dev
github.com

...an average of 10 seconds to render The log output already suggests an optimizable SQL query, view rendering time and a very high number of Allocations: The code involved looks...

...but in this case, it is: class FatModelsController def index result = ActiveRecord::Base.connection.execute(Arel.sql(<<-SQL)) SELECT jsonb_agg( jsonb_build_object('name', indexed_attribute_1) ) AS aggregated_names FROM fat...

...for the down path. However, if you need to some path-specific logic (like SQL UPDATE statements) you can not define up and down methods at the same time.

...is like running the change block from bottom upwards. This is relevant when writing SQL statements: When migrating down, the rename_table statement will be run after the reversible block...

Understand that an expression like User.where(email: 'foo@bar.com') does not make an SQL query. It simply returns a scope object for further chaining with #where, #order, etc.

...will make an SQL query when you use them like an array, e.g. by iterating over them with #each. This may cause the scope to accidentally load its results prematurely...

api.rubyonrails.org

...However, for database-centered stuff like migrations, these fill the gap between writing pure SQL and full ActiveRecord. Hence, the module is included in migrations by default (also see How...

...in a model for example, you have to use the connection: connection = ActiveRecord::Base.connection

...# your SQL Statement results = connection.select_values(sql...

...explains multiple ways to express this condition using ActiveRecord's query interface (without writing SQL). As an example we will use a User that has many Posts: class User < ApplicationRecord...

...FROM posts WHERE user_id IN (3, 7, 11, 19); Option 2: Use an SQL JOIN Post.joins(:user).where('users.trashed = ?', false) This will build a huge join table:

geekytidbits.com

...user id and take only the latest published post for each group. As pseudo sql this could also be written as: SELECT * FROM ( SELECT user_id, posts.* FROM posts

...examples like the above on the web, it may or may not be valid sql! The reason for this is, that many of the modern sql optimizers will not take...

...User.where(id: [1, 2]) filtered_users = User.where(id: [2, 3]) authorized_users.merge(filtered_users).to_sql # => SELECT * FROM users WHERE id IN (2, 3) The merged relation select the users...

...column still works as expected: User.where(id: [1, 2]).where(id: [2, 3]).to_sql # => SELECT* FROM users WHERE id IN (1, 2) AND id IN (2, 3) Alternative: Subselects...

and ActiveRecord will ask the database to lock the row for you. in SQL this looks like SELECT `models`.* FROM `models` WHERE `models`.`id` = 23 LIMIT 1 FOR UPDATE...

...of (hour, minute second) without a day, month or year. Another additional gem? Thus SQL has a time datatype for storing time of day in the format hh:mm:ss...

...day data in your ActiveRecord model, you can simply use :time, which is an SQL datatype. To automatically cast values from and to a Tod::TimeOfDay, use one of the...

Sometimes we write plain SQL queries in migrations so we don't have to mock ActiveRecord classes. These two migrations do the same: class Migration1 < ActiveRecord::Migration[5.2]

...users, :trashed, :boolean update("UPDATE users SET trashed = #{quoted_false}") end end The plain SQL migration is less code, but has a pitfall when combined with a condition. Let's...

...you create records in a migration? The most basic way is to write plain SQL, but since INSERT statements are no pleasant write for Rubyists, here is a simple wrapper...

def insert_record(table, **attributes) attributes.merge!( updated_at: Time.now, created_at: Time.now, ) insert <<-SQL.squish INSERT INTO #{table} (#{attributes.keys.join ', '}) VALUES (#{attributes.values.map(&connection.method(:quote)).join ', '}) SQL end Usage example:

makandra Curriculum

Instead of migrations, could we simply log into the production server's SQL console and alter tables there whenever we need a change? How do we need to...

...existing records. Write this migration in multiple styles: Embedding ActiveRecord models into your migration SQL statements (update "UPDATE actors SET...

...to cache totals in the database, e. g. to sum up many invoices through SQL. As we learned in "Don't pass around unrounded values", these totals should be rounded...

makandra dev

...use a collation to determine the result of comparisons. In most collations, the following SQL statements explain the sorting above: SELECT 'medium' > 'low'; // true SELECT 'medium' > 'critical'; // true

...Adding new values requires a code change Changing existing values requires a data migration SQL queries on array columns have limitations We need to take care of not assigning nil...

...or custom code since we have no build-in dependent: :destroy support in ActiveRecord SQL queries on array columns have limitations Notes The checked option is not set in case...

...make existing records invalid. You can usually review existing upload filenames using a single SQL query. In CarrierWave and Paperclip the filenames of all uploads will usually sit in a...

...as expected (are not matched with a regexp, can't be found with an SQL query, do not print correctly on LaTeX documents, etc), you may be encountering umlauts which...

...come from gems which we don't have control of. Solutions Don't log SQL queries. This is the default behavior of rails but can be changed in the config/environment/production.rb...

...This forces us to set the filter_attributes again to avoid logging PII in SQL queries ActiveSupport.on_load(:active_record) do Rails.application.config.after_initialize do ActiveRecord::Base.filter_attributes = Rails.application.config.filter_parameters

...a longer string, the ActiveRecord validation will pass and then crash when making the SQL statement. The user sees an error box ("Something went wrong") instead of a validation error...

...does not map well to ActiveRecord, you could use Sequel instead of writing plain SQL. If all else fails, we have also used screen scrapping in the past.

...you can use reset: user.posts.reset # discards cache, but does not load anything yet user.posts # SQL query happens to load new state # => [...] Note that reset returns the association/scope. Hence, the above...

...and "USER", this will not trigger a validation error, but may fail with an SQL error due to duplicate index key. You can change Rails' behaviour, by saying

...You Know, for Search" } Indexe Dokumente in Elastic leben in einem Index (analog zu SQL Table) Index erstellen: http PUT :9200/my_documents Index ansehen: http GET :9200/my_documents { "my_documents": { "aliases": { },

...Elasticsearch-Server auf einer Maschine Cluster ist zusammenschluss von Nodes (analog zu einer verteilten SQL Datenbank) Man redet effektiv immer mit dem Cluster Load-Balancing passiert intern automatisch