Shortcut for getting ids for an ActiveRecord scope
You can use .ids
on an ActiveRecord 4+ scope to pluck all the ids of the relation
User.where("users.name LIKE 'Foo Bar'").ids
# same as (since Rails 3.2)
User.where("users.name LIKE 'Foo Bar'").pluck(:id)
If you are stuck on Rails 2.3 you can use
Edge Rider's collect_ids
Show archive.org snapshot
:
User.where("users.name LIKE 'Foo Bar'").collect_ids(:id)
Related cards:
Security issues with hash conditions in Rails 2 and Rails 3
Find conditions for scopes can be given either as an array (:conditions => ['state = ?', 'draft']
) or a hash (:conditions => { 'state' => 'draft' }
). The later is nicer to read, but has horrible security implications in some versions of Ru...
How to create giant memory leaks in AngularJS (and other client-side JavaScript)
This guide shows how to create an AngularJS application that consumes more and more memory until, eventually, the browser process crashes on your users.
Although this guide has been written for Angular 1 originally, most of the advice is relevant...
Collect an array of IDs from any object
The Edge Rider gem will define a method collect_ids
on your ActiveRecord models, scopes, integer scalars and collections, which will return a list of their IDs:
User.last.collect_ids # => [9]
[U...
Collect all values for a given column in an ActiveRecord scope
In modern Rails versions you can also use ActiveRecord's pluck
method.
>> User.active.pluck(:id)
=> [1, 5, 23, 42]
If you are plucking from the `id...
Get Moving With Angular 1.2 Animation and Animate.css
Motion is quickly becoming one of the most important emerging techniques in building a quality user experience on the web. Angular 1.2, currently in release candidate form, provides an overhaul of Angular's animation system and makes it spectacu...
Partially disable animations for Angular 1.4+
If you use Angular 1.4+ together with Angular Animate, all ng-show
, ng-hide
, ng-class
etc. are animated on default.
If you want only some elements to be animated there are 2 possibilities: Either disable animations globally and only run ani...
Find an ActiveRecord by any column (useful for Cucumber steps)
The attached patch lets you find a record by a string or number in any column:
User.find_by_anything('carla')
User.find_by_anything('email@domain.de')
User.find_by_anything(10023)
There's also a bang variant that raises `ActiveRecord...
AngularJS directive to format a text with paragraphs and new lines
If you are using Angular and want something like Rails' simple_format
which HTML-formats a plain-text input into paragraphs and line breaks, this directive is for you.
Any ...
Unexpected behavior when changing both an association and its foreign key attribute in ActiveRecord
When you set both a record's association and that association's foreign key attribute, Rails does not realize you are talking about the same thing. The association change will win in the next save
, even if the foreign key attribute was changed a...
ActiveRecord: Specifying conditions on an associated table
We can use ActiveRecord's where
to add conditions to a relation. But sometimes our condition is not on the model itself, but on an associated model. This card explains mult...