arel

How to override :order defined in a has_many

核能气质少年 提交于 2019-12-02 17:52:42
I have class Authors has_many :books, :order => 'name ASC' I am trying to query all the books sorted by name DESC Authors.books.order('name DESC') but the result is SELECT * FROM .... ORDER BY name ASC, name DESC and the results come back with the name sorted ASC is there a way to remove the original order in the association or override it? Or is specifying an order in a relation a bad idea? using Rails 3.0.3 Ariejan Use reorder : Authors.books.reorder('name DESC') Jon .reorder() has been deprecated in Rails 3.0.3 in favor of .except(:order).order() So use this: Authors.books.except(:order)

How to find records that have duplicate data using Active Record

一笑奈何 提交于 2019-12-02 16:39:32
What is the best way to find records with duplicate values in a column using ruby and the new Activerecord? fl00r Translating @TuteC into ActiveRecord: sql = 'SELECT id, COUNT(id) as quantity FROM types GROUP BY name HAVING quantity > 1' #=> Type.select("id, count(id) as quantity") .group(:name) .having("quantity > 1") Here's how I solved it with the AREL helpers, and no custom SQL: Person.select("COUNT(last_name) as total, last_name") .group(:last_name) .having("COUNT(last_name) > 1") .order(:last_name) .map{|p| {p.last_name => p.total} } Really, it's just a nicer way to write the SQL. This

How do you scope ActiveRecord associations in Rails 3?

安稳与你 提交于 2019-12-02 14:11:41
I have a Rails 3 project. With Rails 3 came Arel and the ability to reuse one scope to build another. I am wondering if there is a way to use scopes when defining a relationship (e.g. a "has_many"). I have records which have permission columns. I would like to build a default_scope that takes my permission columns into consideration so that records (even those accessed through a relationship) are filtered. Presently, in Rails 3, default_scope (including patches I've found) don't provide a workable means of passing a proc (which I need for late variable binding). Is it possible to define a has

Ordering nested has_many :through by count of associations

有些话、适合烂在心里 提交于 2019-12-02 08:31:34
I'm trying to order Tags by order of the descending frequency of their association with Users of a specific Group. (ActiveRecord & Rails 3.2 - I also have Squeel installed if that helps!) Users have Tags, Groups have Users. Groups thus have Tags through Users. class Group < ActiveRecord::Base has_many :users_groups # join table has_many :users, through: :users_groups has_many :tags, through: :users class User < ActiveRecord::Base has_many :users_groups # join table has_many :groups, through: :users_groups has_many :taggings # join table has_many :tags, through: taggings class Tag <

How would I join to a subselect (a scope) using Rails 3 and Arel?

。_饼干妹妹 提交于 2019-12-01 00:58:25
I need to join a table to the a select/group-by query (which includes the same table), and I'd like to do it using Arel. I have a table of :phenotypes which are has_and_belongs_to_many :genes , which are themselves has_and_belongs_to_many :orthogroups . As a result, the relationship between phenotypes and orthogroups are many-to-many. I have two scopes (on Orthogroup) which get all orthogroups associated with a specific phenotype: scope :with_phenotype, lambda { |phenotype_id| where("observations.phenotype_id = ?", phenotype_id). joins("inner join orthologies on (orthologies.orthogroup_id =

How do I use functions like CONCAT(), etc. in ARel?

半腔热情 提交于 2019-11-30 13:39:37
Is there a way to have ARel write (sanitized, possibly aliased, etc.) column names into CONCAT() and other SQL functions? Here's how to do it with AVG() ... ?> name = Arel::Attribute.new(Arel::Table.new(:countries), :name) => #<struct Arel::Attributes::Attribute [...] ?> population = Arel::Attribute.new(Arel::Table.new(:countries), :population) => #<struct Arel::Attributes::Attribute [...] ?> Country.select([name, population.average]).to_sql => "SELECT `countries`.`name`, AVG(`countries`.`population`) AS avg_id FROM `countries`" (yes, I know that avg_id would be the same in every row, just

Rails 3: Arel for NOT EXISTS?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 13:37:15
How do you write a NOT EXISTS in Arel? I'm having trouble translating this query into Arel: SELECT * FROM deals WHERE NOT EXISTS ( SELECT 1 FROM reward_deals WHERE reward_deals.deal_id = deal.id AND NOT ( reward_deals.awarding_type = 'deal' AND reward_deals.deal_id = reward_deals.awarding_id ) ) Pedro Morte Rolo Here is the answer, with strange names because I don't know how to give names for a domain that is for me unknown. deals = Deal.arel_table reward_deals = RewardDeal.arel_table awarding_condition= reward_deals[:awarding_type].eq('deal')\ .and(reward_deals[:deal_id]\ .eq(reward_deals[

OR operator in WHERE clause with Arel in Rails 4.2

半城伤御伤魂 提交于 2019-11-30 09:17:54
问题 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 equivalent to the SQL select * from my_models where (attribute1 = 1 OR attribute2 = 2) In Rails 4.2, the same code generates an SQL query with missing values for it's bind parameters select * from my_models where attribute1 = OR attribute2 = ... and generates an error due to the missing values for the bound

Where can I find good AREL documentation? [closed]

陌路散爱 提交于 2019-11-30 06:05:28
I'm trying to learn as much as I can about AREL. But I'm not sure what to look at. I found some documentation on rubydoc , but it doesn't seem very good in terms of showing what are the "public API"/accessible things I can do. For example, I could not find any information on the "includes" method. So, how exactly to learn most of what AREL has to offer? (i.e. without delving into the source code) I think that this will be useful : https://github.com/brynary/arel And an interesting asciicast : http://asciicasts.com/episodes/215-advanced-queries-in-rails-3 I got tired of reading tests and code,

How to do “where exists” in Arel

戏子无情 提交于 2019-11-30 04:55:44
How do you do a query that includes a "where exists" in Arel? For example on a query like this to show all the suppliers with at least one order: SELECT * FROM suppliers WHERE EXISTS (SELECT * FROM orders WHERE suppliers.supplier_id = orders.supplier_id); I see "exists" in the Arel docs http://rubydoc.info/gems/arel/2.0.7/Arel/Nodes/Exists but I'm having trouble using it. Here you go: suppliers= Supplier.arel_table orders= Order.arel_table suppliers_with_orders = Supplier.where( Order.where(orders[:supplier_id] .eq(suppliers[:id])).exists).to_sql => "SELECT `suppliers`.* FROM `suppliers` WHERE