Questions tagged [active-relation]

A Ruby Relational Algebra Library

Active Relation also known as "Arel" is the Relational Algebra library used in Active Record and Ruby on Rails.

69 questions
107
votes
17 answers

ActiveRecord Query Union

I've written a couple of complex queries (at least to me) with Ruby on Rail's query interface: watched_news_posts = Post.joins(:news => :watched).where(:watched => {:user_id => id}) watched_topic_posts = Post.joins(:post_topic_relationships =>…
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
31
votes
3 answers

Rails/Arel: Selecting all records as an ActiveRecord::Relation

Using Arel in Rails - I'm looking for a way of creating an ActiveRecord::Relation that effectively results in SELECT * FROM table, which I can still manipulate further. For example, I have a model that's split up into multiple categories, and I…
Jeriko
  • 6,547
  • 4
  • 28
  • 40
24
votes
4 answers

How to exclude an array of ids from query in Rails (using ActiveRecord)?

I would like to perform an ActiveRecord query that returns all records except those records that have certain ids. The ids I would like excluded are stored in an array. So: ids_to_exclude = [1,2,3] array_without_excluded_ids = Item. ??? I'm not…
CuriousYogurt
  • 836
  • 2
  • 12
  • 18
17
votes
1 answer

Rails: ActiveRecord where vs merge

First, I am getting the review statuses between particular dates. date_range = Date.parse(@from_date).beginning_of_day..Date.parse(@to_date).end_of_day @review_statuses = ReviewStatus.where(updated_at: date_range) Next, I need to apply an 'AND'…
Rajkaran Mishra
  • 4,532
  • 2
  • 36
  • 61
16
votes
5 answers

ActiveRecord Association select counts for included records

Example class User has_many :tickets end I want to create association which contains logic of count tickets of user and use it in includes (user has_one ticket_count) Users.includes(:tickets_count) I tried has_one :tickets_count, :select =>…
Fivell
  • 11,829
  • 3
  • 61
  • 99
15
votes
4 answers

How do I write a UNION chain with ActiveRelation?

I need to be able to chain an arbitrary number of sub-selects with UNION using ActiveRelation. I'm a little confused by the ARel implementation of this, since it seems to assume UNION is a binary operation. However: ( select_statement_a ) UNION (…
Adam Lassek
  • 35,156
  • 14
  • 91
  • 107
15
votes
4 answers

How do I get Rails to eager load counts?

This is related to a question a year and change ago. I put up an example of the question that should work out of the box, provided you have sqlite3 available: https://github.com/cairo140/rails-eager-loading-counts-demo Installation instructions (for…
Steven
  • 17,796
  • 13
  • 66
  • 118
14
votes
2 answers

Is there a DRY way to pass bind variables to an AR Relation?

I have code like this: t = "%#{term}%" where('name like ? or email like ? or postcode like ?', t, t, t) As you can see it's performing a search across several fields. Is there a way to avoid the duplicated t? It's making me feel dirty. Thanks
superluminary
  • 47,086
  • 25
  • 151
  • 148
12
votes
3 answers

Difference between ActiveRecord and ActiveRecord::Relation objects

I have searched but not able to find the brief explanation for the difference between ActiveRecord and ActiveRecord::relation object. I have understand that ActiveRecord is the single object find by something like User.find(1) And…
12
votes
1 answer

Turn SQL query into ActiveRecord Relation

How can I turn the following SQL query into an ActiveRecord relation so that I can expand on it with scopes? WITH joined_table AS ( SELECT workout_sets.weight AS weight, workouts.user_id AS user_id, workouts.id AS workout_id, …
MunkiPhD
  • 3,636
  • 1
  • 29
  • 51
10
votes
3 answers

dynamically get rails association

If I have an association name as a string, is there a way I can get a handle on the association object? For example: o = Order.first o.customer will give me the customer object that this order belongs to. If I have: o = Order.first relationship =…
Sean
  • 1,078
  • 14
  • 25
7
votes
2 answers

Arel Deprecation Warning when running rake db:create

I'm trying to create an app in Rails 3.1 with mysql2 v 0.2.6. When running rake db:create, I get the following error: DEPRECATION WARNING: Arel::Visitors::VISITORS is deprecated and will be removed. Database adatpers should define a visitor_for…
7
votes
1 answer

multiple belongs_to relationships between two classes in Rails

I have a Transaction class. Each object of this class includes one issuing account, one sending account and one receiving account. Each of these is an instance of Account class. In my Transaction table, I have issuer_id, sender_id and…
AdamNYC
  • 19,887
  • 29
  • 98
  • 154
7
votes
7 answers

Rails 3: How to get all Posts which ids are not in a given list?

To get all posts with publisher_id equals to 10, 16, or 17, I do: Post.where(:publisher_id => [10, 16, 17]) How would I get all posts with publisher_id not equals to 10, 16, or 17 (i.e. all possible ids besides those three) ?
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
7
votes
3 answers

How will ActiveRelation affect rails' includes() 's capabilities?

I've looked over the Arel sources, and some of the activerecord sources for Rails 3.0, but I can't seem to glean a good answer for myself as to whether Arel will be changing our ability to use includes(), when constructing queries, for the…
Tim Snowhite
  • 3,736
  • 2
  • 23
  • 27
1
2 3 4 5