Questions tagged [arel]

Arel is a Relational Algebra for Ruby. It simplifies the generation complex of SQL queries and it adapts to various RDBMS systems.

Arel is a Relational Algebra for . It simplifies the generation complex of SQL queries and it adapts to various RDBMS systems. More info on project's page. The Arel gem is supported in framework versions 3.0 and up, however for the 2nd version of Rails the fake_arel gem shell be used.

723 questions
0
votes
2 answers

Representation of NOT IN combinations

I would like to use squeel in order to build query like: SELECT * from `table` WHERE (`field1`, `field2`) NOT IN ( (1,"value_a"), (2,"value_b"), (3,"value_a"), ... ) I want to know if there is any way to compare multiple fields with an…
Krule
  • 6,468
  • 3
  • 34
  • 56
0
votes
1 answer

Converting SQL into Arel or Squeel

tl;dr How to convert below SQL to Arel(or whatever is considered standard in Rails) @toplist = ActiveRecord::Base.connection.execute( 'select ci.crash_info_id, count(distinct(user_guid))[Occurences], c.md5 from crashes…
Jack Juiceson
  • 830
  • 4
  • 12
  • 24
0
votes
2 answers

How to perform a select if duplicates do not exist?

I'm looking for the best possible way to select an object if no duplicates exists. I have a method that is duplicating an array of objects. But there's no need to duplicate them if a duplicate already exists. How can I initially select my array of…
Trip
  • 26,756
  • 46
  • 158
  • 277
0
votes
5 answers

Reusing activerecord scopes on has many through associations

Say I have a few activerecord models in my rails 3.1 project that look like this: class Component < ActiveRecord::Base has_many :bugs end class Bug < ActiveRecord::Base belongs_to :component belongs_to :project scope :open,…
iainbeeston
  • 1,851
  • 1
  • 21
  • 20
0
votes
1 answer

Rails Brakeman SQL injection warning when using Arel syntax

In my Rails 3.2 app, Brakeman 1.8.3 raises a High confidence SQL injection warning for the following code in a model: micropost.rb def self.from_users_followed_by(user) followed_user_ids = Relationship.select(:followed_id). …
Paul Fioravanti
  • 16,423
  • 7
  • 71
  • 122
0
votes
2 answers

ActiveRecord find by number of associated records

I'd like an ActiveRecord query that lets me return all the users that have zero email_sessions. Here're my models: class User has_many :email_sessions end class EmailSession belongs_to :user end It would be pretty straight-forward to do…
Aaron Vegh
  • 5,217
  • 7
  • 48
  • 75
0
votes
1 answer

This returns the records I want, but not the size/count/length like I would expect?

I have the following scope on my model. I created it so I could get a list of parent objects that have children that match the some_id_on_child value 123. This seems to return the right records. scope :light, joins(:children).where(children: {…
Altonymous
  • 783
  • 7
  • 25
0
votes
0 answers

Coverting a sql query to Arel

How would I convert the following sql query to Arel? SELECT SUM( LEAST( SPONSORSHIPS.AMOUNT_PER_LAP * ( SELECT SUM(LAPS.POINTS) FROM LAPS WHERE LAPS.CAMPAIGN_ID = :j_id …
Cyrus
  • 3,687
  • 5
  • 35
  • 67
0
votes
1 answer

How to make a field unique by the result of a coalesce operation?

class Person validates :full_name, presence: true validates :display_name, unique: ??? attr_acessible :full_name, :display_name end Full names can not be unique, but the display name must be unique. If I want to have the full_name as a…
michelpm
  • 1,795
  • 2
  • 18
  • 31
0
votes
2 answers

Get all records of a model where its association is not null and the association meets certain conditions

I am using Rails 3.1.0 I have two models, A and B. A has_one B, but B can be null. B belongs_to A. B has a boolean field called "visible." I want to get all A records that have a non-null B and a B with "visible" set to true. What is a concise…
mushroom
  • 6,201
  • 5
  • 36
  • 63
0
votes
1 answer

Why is Rails 3 not excluding 'or'-ed condition when overriding scopes?

Consider these scopes on an ActiveRecord model: scope :onsale, where(:sales_state => "onsale") scope :outdated, where(:sales_state => "outdated") scope :onsale_or_outdated, where(" sales_state = 'onsale' OR sales_state = 'outdated' ") Why is it…
Cristian
  • 5,877
  • 6
  • 46
  • 55
0
votes
1 answer

How would I do this query with Arel?

I have a User model and an Item model. I want to rank users according to the value of the items they have. I want to do the equivalent of this query: SELECT rank() OVER (ORDER BY grand_total DESC), u.*, grand_total FROM users AS u JOIN (SELECT…
phaedryx
  • 2,024
  • 3
  • 16
  • 24
0
votes
1 answer

Joining multiple tables while accumulating conditions

I have a class method Relationship.with_attributes that I use to build up a complex scope based on multiple input parameters to return Relationship objects associated with products that match certain attributes. In most cases, the information I…
jredburn
  • 798
  • 5
  • 9
0
votes
3 answers

How can I get an ActiveRecord::Association to initialize a parent's auto-loaded children's parent references to point at the actual parent object?

This is in Ruby 1.9.3p194, with Rails 3.2.8 app/models/owner.rb: class Owner < ActiveRecord::Base attr_accessible :name has_many :pets end app/models/pet.rb: class Pet < ActiveRecord::Base attr_accessible :name, :owner belongs_to…
Chris
  • 632
  • 4
  • 11
  • 18
0
votes
1 answer

find members with canceled subscriptions

So, Members have many subscriptions. A member is canceled if the last subscription canceled_at is not nil. But I can't seem to get the query to work right, to find all members with presently canceled subscriptions. I'm doing this in the member…
user308096