Posted over 4 years ago. Visible to the public. Repeats. Linked content.

What edge_rider gives you

edge_rider is Power tools for ActiveRecord relations (scopes). Please note that some of the functions edge_rider provides have native implementations in newer rails versions.

Relation#traverse_association(*names)

Edge Rider gives your relations a method #traverse_association which returns a new relation by "pivoting" around a named association. You can traverse multiple associations in a single call. E.g. to turn a relation of posts into a relation of all posts of their authors:

Copy
posts = Post.where(:archived => false) posts_by_same_authors = posts.traverse_association(:author, :posts)

Relation#collect_ids

You should not use relation.collect(&:id) for this because a call like that will instantiate a potentially large number of ActiveRecord objects only to collect its ID.

Edge Rider has a better way. Your relations gain a method #collect_ids that will fetch all IDs in a single query without instantiating a single ActiveRecord object:

Copy
posts = Post.where(:archived => false) post_ids = posts.collect_ids

Implemenation note: In Rails 3.2+, #collect_ids delegates to #pluck, which can be used for the same purpose.

Relation#collect_column(name, options)

You should not use relation.collect(&:column) for this because a call like that will instantiate a potentially large number of ActiveRecord objects only to collect its column value.

Edge Rider has a better way. Your relations gain a method #collect_column that will fetch all column values in a single query without instantiating a single ActiveRecord object. If you only care about unique values, use the :distinct => true option:

Copy
posts = Post.where(:archived => false) distinct_subjects = posts.collect_column(:subject, :distinct => true)

Implementation note: In Rails 3.2+, #collect_column delegates to #pluck, which can be used for the same effect.

Relation#to_sql

Copy
# Rails 2 scope Post.scoped(:conditions => { :id => [1, 2] }).to_sql # => "SELECT `posts`.* FROM `posts` WHERE `posts.id` IN (1, 2)"
Copy
# 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.

Relation#to_id_query

Copy
Site.joins(:user).where(:users => { :name => 'Bruce' }).to_id_query

to_id_query will immediately run an SQL query where it collects all the IDs that match your relation:

Copy
SELECT sites.id FROM sites INNER JOIN users WHERE sites.user_id = sites.id AND users.name = 'Bruce'

It now uses these IDs to return a new relation that has no joins and a single condition on the id column:

Copy
SELECT * FROM sites WHERE sites.user_id IN (3, 17, 103)

Model.preload_associations(object, associations)

Edge Rider gives your model classes a method .preload_associations. The method can be used to preload associations for loaded objects like this:

Copy
@user = User.find(params[:id]) User.preload_associations [@user], { :threads => { :posts => :author }, :messages => :sender }

Relation#origin_class

Edge Rider gives your relations a method #origin_class that returns the class the relation is based on. This is useful e.g. to perform unscoped record look-up.

Copy
Post.recent.origin_class # => Post

Model.scoped

Edge Rider ports Model.scoped forward to Rails 4+ (taken from activerecord-deprecated_finders).

Copy
User.scoped # just calls User.all in Rails 4 User.active.scoped(conditions: { admin: true })

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Avatar
Dominik Schöler
Last edit:
over 2 years ago
by Judith Roth
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more