arel

Extract records which satisfy a model function in Rails

 ̄綄美尐妖づ 提交于 2019-12-13 08:34:44
问题 I have following method in a model named CashTransaction . def is_refundable? self.amount > self.total_refunded_amount end def total_refunded_amount self.refunds.sum(:amount) end Now I need to extract all the records which satisfy the above function i.e records which return true . I got that working by using following statement: CashTransaction.all.map { |x| x if x.is_refundable? } But the result is an Array . I am looking for ActiveRecord_Relation object as I need to perform join on the

Translate complex SQL query using SET to Arel or Squeel

[亡魂溺海] 提交于 2019-12-13 04:55:24
问题 How can I translate the given query using AREL or Squeel: SET @start = '2013-05-14'; SET @end = '2013-11-01'; SET @days = DATEDIFF(@end, @start); SET @UnusedDays = 0; SELECT @UnusedDays := DATEDIFF(end_at,@end) FROM PERIODS WHERE (@end > start_at AND @end <= end_at); SELECT @UnusedDays := @UnusedDays + DATEDIFF(@start, start_at) FROM PERIODS WHERE (@start >= start_at AND @start < end_at); SELECT @days + @UnusedDays - SUM(DATEDIFF(end_at, start_at)) AS Shortfall FROM PERIODS WHERE (@start >=

Ordering by number of associations in common even if none (rails)

天大地大妈咪最大 提交于 2019-12-12 05:05:20
问题 I am working on a project which has a very similar quandary to this ask: Ordering by number of associations in common (Rails) The asker of said ask (Neon) writes, BACKGROUND: I have Posts and Users, and both have many Communities. OBJECTIVE: For any given User I'd like to return a collection of Posts, ordered by how many communities the post has in common with the user (posts with more communities in-common being higher up) Unfortunately, the solution only includes posts that have at least

Multiple CTEs with Arel

拟墨画扇 提交于 2019-12-12 04:37:20
问题 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.from_id = from.id and rates.to_id = to.id How can I convert this to Arel? I looked into Arel CTE, but there are no examples of using multiple aliases like in the query above. 回答1: This should do the trick: from_table = Arel::Table.new(:from) to_table = Arel::Table.new(:to) rates_table = Arel::Table.new(

Convert SQL statement to Arel Rails query

自古美人都是妖i 提交于 2019-12-11 16:56:13
问题 So, let's get straight: I'm struggling to days do convert this SQL Statement: select * from ( select distinct on (program_id) editions.*, programs.* from editions inner join programs on editions.program_id = programs.id where programs.station_id = 1) as editionsprograms order by fixed desc, published_at desc, time desc limit 4; to an Arel rails query. Someone has any idea about how could make an Arel query that would have the same effect of the above SQL? 回答1: You can use the from method to

Sorting in named_scope through 2 tables with condition

老子叫甜甜 提交于 2019-12-11 12:18:30
问题 I am looking for solution how to sort by attribute which is two association levels deep and also have an condition. I have order model which HAS TO have association with Shop model OR Warehouse model both of these models are associated with country which has a name. My goals are: Scoped the orders and sort by country name. Result has to be a ActiveRelation object And the main goal is use this scope for MetaSearch gem for view helper sort_link class Order < ActiveRecord::Base belongs_to :shop

Rails Arel joins with where condition on joined table

末鹿安然 提交于 2019-12-11 07:14:27
问题 I'm trying to convert the following Rails where clause to use Arel, mostly to take advantage of the or method that Arel provides. Post model class Post belongs_to :user end User model class User has_many :posts end I'm looking for posts posted by Mark. This is the Rails Query: Post.joins(:user).where(users: { first_name: 'Mark' }) I need to convert this query with Arel. Thanks in advance! 回答1: This should do it. # Generate Arel tables for both posts = Arel::Table.new(:posts) users = Arel:

Rails sum on AssociationRelation attribute is incorrect if association has limit clause

余生颓废 提交于 2019-12-11 06:17:42
问题 I have a method that computes stats (mainly sums) on a number of float attributes in a model. The models class GroupPlayer < ActiveRecord::Base belongs_to :group has_many :scored_rounds has_many :rounds, dependent: :destroy end class Round < ActiveRecord::Base belongs_to :group_player end class ScoredRound < Round # STI end The method that provides stats on up to 4 float attributes that is called from a other methods, depending if I'm getting stats for one player or a group of players. An

Express a CTE using Arel

旧街凉风 提交于 2019-12-11 06:17:41
问题 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 would like to add a CTE that would result in this sql: WITH lookup AS ( SELECT the_geom FROM lookup WHERE slug = 'foo-bar' ) SELECT * from listings, lookup WHERE listings.price > 6000000 AND ST_within(listings.the_geom, lookup.the_geom) I would like to express this sql inclusive of the CTE using Arel and

How do I find records based on attributes of a many-to-many-related model?

邮差的信 提交于 2019-12-11 03:26:18
问题 Models... InternalUser has_many :internal_user_roles has_many :roles, :through => :internal_user_roles InternalUserRole belongs_to :internal_user belongs_to :role Role has_many :internal_user_roles has_many :internal_users, :through => :internal_user_roles Using the new ActiveRecord query API, how would I find all the InternalUser s with the "ADMIN" role? In other words, how do I generate this query... SELECT * FROM internal_users i, internal_user_roles ir, roles r WHERE i.id = ir.internal