arel Questions

3

So I got this crazy idea that I wanted to apply a scope to an included association. This is what I figured out, and it seems to work just fine: class Event < ActiveRecord::Base has_many :races...
Cayser asked 1/2, 2011 at 5:4

1

We recently upgraded to Rails 4.2 from Rails 4.1 and are seeing problems with using Arel + Activerecord because we're getting this type of error: ActiveRecord::StatementInvalid: PG::ProtocolViolat...
Knocker asked 10/11, 2015 at 23:25

1

I have this snippet of ActiveRecord: scope = Listing.where(Listing.arel_table[:price].gt(6_000_000)) The resulting sql: SELECT listings.* FROM listings where listings.price > 6000000 I wou...

1

Solved

I need to generate SQL using Arel with the form SELECT c2.user_id, MAX(c2.created_at) as max_created_at FROM comments AS c2 GROUP BY c2.user_id to be used as a subquery in a larger query SELECT...
Plastered asked 9/9, 2017 at 13:26

2

Solved

I'm trying to find a method to take a particular relation and move it to the end of the array. Basically, I have a current_account and I want to take this account and move it to the end of the acco...
Lesotho asked 3/9, 2017 at 18:13

1

Solved

I have an sql query of the following format: with from as (#{select * query}), to as (#{another select *}), rates as (#{yet another select *}) select rates.* from rates, from, to where rates....
Reindeer asked 13/7, 2017 at 21:42

1

Solved

Let us we have a Rails 4.2.x app and we have two tables posts and authors, and we want to use Arel to get the posts authored by an author with name == 'Karl'. (In this case we could be happy with ...
Gabie asked 16/3, 2017 at 17:28

5

Solved

I have a table of around 100 Users and I also have an array of user ids. What I wanted to do is show all users who are not a part of this array of user ids. When I do something like this User.whe...
Frankiefrankincense asked 18/10, 2012 at 1:57

1

Solved

On my postgres db, I have a primary key using a UUID. Sample setup below class Visit # primary key id: uuid has_many :connections, as: :connectable has_many :payments, through: :connections end...
Yonina asked 3/3, 2017 at 17:11

2

--Edit-- I wanted to simplify this question. With this model structure: has_one :pickup_job, class_name: 'Job', source: :job has_one :dropoff_job, class_name: 'Job', source: :job What I want to...
Authoritative asked 18/2, 2017 at 0:13

2

Solved

I have a HABTM relationship between the Theme and Quote models. The themes index view displays the count of quotes associated with each theme. I'd like to add a Ransack sort_link on that column, so...
Trajan asked 1/2, 2017 at 16:36

5

Solved

Using the rails 3 style how would I write the opposite of: Foo.includes(:bar).where(:bars=>{:id=>nil}) I want to find where id is NOT nil. I tried: Foo.includes(:bar).where(:bars=>{:id...
Chavarria asked 23/11, 2010 at 2:56

7

Solved

I have a default scope on products due to information security constraints. class Product < ActiveRecord::Base has_many :photos default_scope where('visible = 1') end In my associated Phot...
Bogusz asked 21/1, 2011 at 11:16

1

I`ve got a model with several taxonomies using acts-as-taggable-on class Film < ActiveRecord::Base attr_accessible :mood_list, :genre_list, :tag_list, :country_list acts_as_taggable_on :count...

0

I'm fairly new to AREL, and have had trouble finding references to using Postgres JSONB with AREL. How might I convert this SQL into an AREL statement? SELECT tag, count(tag) FROM people p, j...
Cytology asked 6/9, 2016 at 9:50

2

How can I give an alias name for e.g. includes()? Following is given: User: active record model Student: active record model, inherits from User (STI) Teacher: active record model, inherits from...

1

I'm using ruby '2.3.0' and 'rails', '3.2.22.2'. I need a little help & explanations about a query I've made. Here's my models: class AssessmentRaw < ActiveRecord::Base belongs_to :session...
Celestina asked 1/5, 2016 at 12:11

4

Solved

Do association methods, such as those defined by has_many and belongs_to utilize ActiveRecord::Relation? If so, is it possible to get the ActiveRecord::Relation object that is being used. We're ...
Witness asked 14/11, 2010 at 8:21

1

I need to form a query in Arel which had a CAST operation earlier. The original query was like : select * from tablename where tablename.anniversary >= CAST(STR_TO_DATE(?,'%d-%m-%Y-%k-%i-%s') as ...
Tend asked 14/9, 2015 at 12:16

3

The following code constructed a valid where clause with an OR operator in Rails 4.1 MyModel.where( MyModel.where(attribute1: 1, attribute2: 2).where_values.reduce(:or) ) Which is roughly equiv...
Malignant asked 23/12, 2014 at 20:15

3

Converting a Rails 2 application to Rails 3, I have to replace the gem searchlogic. Now, using Rails 3.2.8 with the gem Ransack I want to build a search form which uses an existing scope. Example: ...
Asperges asked 18/10, 2012 at 8:6

2

Solved

I have a scope defined as follows: scope :ignore_unavailable, lambda { where([ "Item.id NOT IN (SELECT id FROM Cars WHERE Cars.status = 'NA'" ]) } Currently its using hardcoded tables names. H...
Valleau asked 5/9, 2014 at 19:9

10

Solved

How can you combine 2 different conditions using logical OR instead of AND? NOTE: 2 conditions are generated as rails scopes and can't be easily changed into something like where("x or y") directl...
Howund asked 2/11, 2011 at 5:57

2

Solved

I'm trying to delete all the organizations that no longer have any users. Using the below code, I can find all the records I wish to delete: Organization.includes(:users) .where(users: { id: nil...
Lamrouex asked 16/10, 2015 at 21:45

3

Solved

There are situations where ActiveRecord sets the alias table name if there are multiple joins with the same table. I'm stuck in a situation where these joins contain scopes (using 'merge'). I have ...
Jackson asked 23/7, 2014 at 21:55

© 2022 - 2024 — McMap. All rights reserved.