arel

Case Statements in ARel

孤者浪人 提交于 2021-01-28 19:06:45
问题 I'm trying to clean up an ActiveRecord code written with MySQL case statements using ARel. I know Arel is able to handle case statements, but not sure how to use it. So I have an orders table with quantity and user_id(foreign key) columns. I'm dynamically computing the price of orders as there are different pricing schemes based on the quantity ordered. This is what the AR code looks like: def orders_with_price <<-SQL orders.*, SUM(CASE orders.quantity WHEN 2 THEN 500 * orders.quantity WHEN 5

How to fetch distinct values with arel/relational algebra and has_many :through

安稳与你 提交于 2020-02-02 07:22:52
问题 When I try to display all movies that a person is in, and they have more than 1 role (director,writer,actor) in a movie, I get multiple lines for that movie. If I add .select('DISTINCT id') or movies.* to try and eliminate the dups I get the following error: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT id FROM movies INNER JOIN movie_people ON movies .id = movie_peop' at line 1:

How to fetch distinct values with arel/relational algebra and has_many :through

孤街醉人 提交于 2020-02-02 07:22:15
问题 When I try to display all movies that a person is in, and they have more than 1 role (director,writer,actor) in a movie, I get multiple lines for that movie. If I add .select('DISTINCT id') or movies.* to try and eliminate the dups I get the following error: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT id FROM movies INNER JOIN movie_people ON movies .id = movie_peop' at line 1:

How to sum a grouped column in AREL

喜夏-厌秋 提交于 2020-01-17 05:58:45
问题 in Rails 3/AREL Model.group(:label).sum(:value) will do SELECT sum(value), label from model_table group by label I am trying to find the AREL way of doing SELECT sum(value) from (select value, label from model_table group by label) which is Model.group(:label).map(&:value).sum using ruby to sum, how do I do this in SQL/AREL 回答1: Not sure this makes sense. Your core SQL is not valid: SELECT value, label FROM model_table GROUP BY label You cannot have a GROUP BY without an aggregation function

Cannot programmatically combine AND and OR conditions using Arel

白昼怎懂夜的黑 提交于 2020-01-15 10:34:12
问题 Given the SQL conditions cond1, cond2 and cond3 generated using Arel operators (.eq for example), I cannot seem to use Arel to produce the SQL: SELECT * FROM <table> WHERE (cond1 AND cond2) OR cond3 This is because to AND conditions together you use .where(), but you can't then .or() the result of .where(). You can only .or() conditions together inside a .where(). I.e. .where and .or are not on the same "level", I would guess one needs a dedicated .and() method on the same level as .or().

Cannot programmatically combine AND and OR conditions using Arel

a 夏天 提交于 2020-01-15 10:31:13
问题 Given the SQL conditions cond1, cond2 and cond3 generated using Arel operators (.eq for example), I cannot seem to use Arel to produce the SQL: SELECT * FROM <table> WHERE (cond1 AND cond2) OR cond3 This is because to AND conditions together you use .where(), but you can't then .or() the result of .where(). You can only .or() conditions together inside a .where(). I.e. .where and .or are not on the same "level", I would guess one needs a dedicated .and() method on the same level as .or().

Building a subquery with ARel in Rails3

元气小坏坏 提交于 2020-01-06 02:20:34
问题 I am trying to build this query in ARel: SELECT FLOOR(AVG(num)) FROM ( SELECT COUNT(attendees.id) AS num, meetings.club_id FROM `meetings` INNER JOIN `attendees` ON `attendees`.`meeting_id` = `meetings`.`id` WHERE (`meetings`.club_id = 1) GROUP BY meetings.id) tmp GROUP BY tmp.club_id It returns the average number of attendees per meeting, per club. (a club has many meetings and a meeting has many attendees) So far I have (declared in class Club < ActiveRecord::Base): num_attendees = meetings

Generating a subquery with Arel to get an average of averages

风格不统一 提交于 2020-01-04 17:49:29
问题 Lets say I have a few of tables orders = Arel::Table.new :orders stores = Arel::Table.new :stores managers = Arel::Table.new :managers And a manager has many stores and a store has many orders. One day I want to query for the average total across orders where a manager works. Oh, I want to group that by the store. So to be clear, I want to get the average order total for a manager, for each of the stores they work at. And let's assume we've looked up our manager: manager = Manager.find(some

Generating a subquery with Arel to get an average of averages

笑着哭i 提交于 2020-01-04 17:47:02
问题 Lets say I have a few of tables orders = Arel::Table.new :orders stores = Arel::Table.new :stores managers = Arel::Table.new :managers And a manager has many stores and a store has many orders. One day I want to query for the average total across orders where a manager works. Oh, I want to group that by the store. So to be clear, I want to get the average order total for a manager, for each of the stores they work at. And let's assume we've looked up our manager: manager = Manager.find(some

In Arel, how can I combine a bunch of conditions with OR?

家住魔仙堡 提交于 2020-01-04 13:34:13
问题 I have a setup similar to this: table = self.arel_table nodes = [1,2,3].collect do |c| table[:foo].eq(c).and( table[:bar].eq(c) ) end I then want those condition fragments combined to end up with SQL similar to: WHERE (foo = 1 AND bar = 1) OR (foo = 2 AND bar = 2) OR (foo = 3 AND bar = 3) The closest I have so far is: nodes.inject(nodes.shift) {|memo, node| memo.or( Arel::Nodes::Grouping.new(node) ) } I've tried Arel::Nodes::Grouping.new in different ways but it never groups how I want. 回答1: