Find records with a Range condition
You can find ActiveRecord models by using a
Range
Show archive.org snapshot
as its conditions:
User.scoped(:conditions => { :id => 3..5 })
This will generate the following query:
SELECT * FROM `users` WHERE (`users`.`id` BETWEEN 3 AND 5)
This also means that all your scopes that take an array of allowed values and use condition hashes, automagically work for Ranges, too.
Related cards:
Select a random table row with ActiveRecord
Use this scope:
class Stick
named_scope :shuffled, lambda {
last_record = last
{ :conditions => [ 'id >= ?', rand(last_record.id) ] } if last_record
}
end
You can now pick a random stick by saying
Stick.s...
Find geocoded records that are close to a location (radius search)
When you have objects in your database that hold latitude and longitude and you want to find others that are close to given coordinates you can use the Graticule gem.
Graticule
Graticule of...
Hack of the day: Find all classes that define a method with a given name
If (for some reason that you don't want to ask yourself) you need to know all classes that define a method with a given name, you can do it like this:
def classes_defining_method(method_name)
method_name = method_name.to_sym
Objec...
ActiveRecord: Creating many records works faster in a transaction
When you need to insert many records into the same table, performance may become an issue.
What you can do to save time is to open a transaction and save multiple records within that transaction:
transaction do
500.times { Model.create!...
Rails: How to find records with empty associations
Imagine these models and associations:
class Deck < ApplicationRecord
has_many :cards
end
class Card < ApplicationRecord
belongs_to :deck, optional: true
end
Now you want to find all Decks without any Card or all Cards without a...
Using Capybara finder methods with arbitrary matching conditions
Capybara has a variety of finder methods like find_button
to help you look up DOM elements. There are also matchers ...
How to use Simplecov to find untested code in a Rails project with RSpec and Cucumber
Simplecov is a code coverage tool. This helps you to find out which parts of your application are not tested.
Integrating this in a rails project with rspec, cucumber and parallel_tests is easy.
- Add i...
How to: Benchmark an Active Record query with a Ruby script
Recently I needed to benchmark an Active Record query for performance measurements. I wrote a small script that runs each query to benchmark 100 times and calculates the 95th percentile.
Note: The script requires sudo permissions to drop RAM cach...
Scope to records with a given state in state_machine
The state_machine gem ships with a scope with_state
. This scope has some problems in complex queries or scope chains.
Use this instead:
named_scope :having_state, lambda { |*state_or_states|
state_or_states = Array.wrap(state_or_stat...
Concurrency issues with find-as-you-type boxes
Find-as-you-type boxes are usually built by observing changes in a text field, and querying the server via AJAX for search results or suggestions when the field has changed.
A common problem with this implementation is that there is no guarantee ...